Oxaric’s Blog

A compendium of amazing things…

Make A BASH Script Globally Executable

Posted by oxaric on December 4, 2008




Bash scripts are great! They make life a lot easier. But they can be a pain to drag around using mv and cp to get them to the right directory so you can execute them directly using: ~$ ./myscript.sh. Thankfully there is a much better way to run often used Bash scripts.


First, before we start you need to make sure the Bash script you’re interested in is executable. To be executable the script file needs the proper permission to run on your system. The command needed to give a file permission to run is chmod +x followed by the name of your script file.


This looks like:

~/test> chmod +x myscript.sh



Once your script is executable you need to copy it to a directory that your system expects to contain executable scripts and code. On most systems you will have a choice between two directories. If you are the only user of your system you can cpy your script to either /usr/bin or /usr/local/bin. If you share your system with other people it’s best to copy your script to /usr/local/bin. You will most likely need super-user privileges to copy your script to either of these directories so you’ll most likely need to use the sudo command or an equivalent. The sudo command will give you temporary super-user privileges and allow you to copy the script. Now you can directly copy the script into one of the stated directories but it is my preference to remove the .sh from my script file names before copying them.


Here are two script file copy examples:

~/test> sudo cp myscript.sh /usr/bin/myscript
~/test> sudo cp myscript.sh /usr/local/bin



Once you copy your script file it will be executable from any directory in your system. Simply type the name of your script file, hit enter, and your script will execute. Give it a try!

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: