Basics for running your own scripts

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Slippery Path

The shell thus makes sure that the same, well-defined commands get executed each time, unless you explicitly request something else with the dot-slash combo. If you're still not a fan of this method, you can always add the current directory in the $PATH variable. Although I will tell you how to do exactly that in the next paragraphs, you do it at your own risk. Ultimately, getting used to using dot-slash is a way better.

To include the current directory into your $PATH , depending on the Linux distribution and shell version, you must tweak the variable with entries in the ~/.profile or ~/.bashrc file in your home directory. Add the export PATH="$PATH:." command at the end of the file (Listing 7, first line) and start a new terminal window. The new path will be like the one on the last line of Listing 7.

Listing 7

New Path

$ echo 'export PATH="$PATH:.' >> ~/.profile
[... new terminal ...]
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:\
  /usr/bin:/sbin:/bin:/usr/games:.

As you can see, the end of the path has a dot for the current directory, so you can omit the dot-slash (Listing 8). With the reference to the current directory at the end of $PATH (remember that Bash parses $PATH in order), you minimize the risk of running into the malicious behavior described earlier.

Listing 8

Reference the Current Directory

tux@computer:~$ cd tmp
tux@computer:~/tmp$ sample.sh
This is only a sample script.

~/bin and /usr/local/bin

However, there's also a so-called canonical way of running scripts without adding path information. The ~/bin/ (i.e., a bin directory in your home directory) and /usr/local/bin/ directories provide meaningful alternatives as used by package management for installing applications. Most distributions add these directories to $PATH automatically so that scripts and programs stored in them are executable without needing a path. You can also create a symbolic link to them from installed applications in /opt .

If the ~/bin/ directory is missing in your home directory, then simply create it, and be sure to add an entry for it in ~/.profile (Listing 9), which the system runs when starting the shell. A newly opened terminal window will then have $HOME/bin/ as the first entry in the path, and scripts in them will be located automatically.

Listing 9

Add ~/bin/

# In case a private bin directory exists
# include it in $PATH
if [ -d "$HOME/bin" ] ; then
  PATH="$HOME/bin:$PATH"
fi

If you want all your system users to run your scripts or small programs, copy them with root privileges to /usr/local/bin/ or symlink them (ln -s <source> <destination> ) there. This directory is always in $PATH and the package manager never puts data or files in it, so that they don't run afoul of your script. The directory remains a safe haven.

Buy this article as PDF

Express-Checkout as PDF

Pages: 2

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content