CodeSpud

Use Export Command To Help Linux Find Your Scripts

September 25, 2009

linuxbashscripts

You compiled and installed a linux binary or made a nifty script but you don’t want to mess the server bin tree. So you placed it in an isolated folder like so:

Level: Beginner, Intermediate

Yeah your a rockstar!
  /usr/local/myscripts/really/great/work/bin/myutility

You go on with business as usual. But soon you got tired of typing the whole path or changing folders every time you need the app. You could simplify your life by making a symbolic link(shortcut) or a wrapper script and place it on a more convenient path like /bin or /usr/bin. But you realize that would be defeating your original intent. what do you do?


Simple. Do what experts do. Use “export” to modify the PATH environment variable.

> help export

export: export [-nf] [name[=value] …] or export -p
NAMEs are marked for automatic export to the environment of subsequently executed commands. If the -f option is given, the NAMEs refer to functions. If no NAMEs are given, or if `-p’ is given, a list of all names that are exported in this shell is printed. An argument of `-n’ says to remove the export property from subsequent NAMEs. An argument of `–‘ disables further option processing.

To get an idea, try this command on the console:

> echo $PATH

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

The above value is a typical “root” PATH for a fresh text-only CentOS installation – so don’t fret if you got something different. This will grow in time the more you install applications. Anyway, back to business, now you want to add your “incredible” scripts. Run the ff:

> export PATH=$PATH:/usr/local/myscripts/really/great/work/bin

Done, now see if Linux can find your script or binary.

> which myutility

/usr/local/myscripts/really/great/work/bin/myutility

Success! Now test your script.

> myutility

If your script throws an error, you may need to work on it. It may be using hard coded relative paths. Sometimes using absolute paths can do the trick.

Yahoo! It works! Now lets take it a bit further, lets configure your system so you don’t need to keep typing the export-PATH command. Open the ~/.bash_profile file in your favorite text editor.

> vi ~/.bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME

Note the tilde(~) symbol, this is just shorthand for your home folder. Lets say your username is iamgenius, that should translate to /home/iamgenius/.bash_profile

Same as what you did earlier, append your script folder to the PATH construct, as shown below

PATH=$PATH:$HOME/bin:/usr/local/myscripts/really/great/work/bin

Now your done, the next time you login, the new “PATH” should kick in.

Congratulations! Now go play with your scripts!

Download a PDF copy

By @codespud  
DISCLAIMER This is my personal weblog and learning tool. The content within it is exactly that – personal. The views and opinions expressed on the posts and the comments I make on this Blog represent my own and not those of people, institutions or organisations I am affiliated with unless stated explicitly. My Blog is not affiliated with, neither does it represent the views, position or attitudes of my employer, their clients, or any of their affiliated companies.