Use Export Command To Help Linux Find Your Scripts
· 3 min read
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/myutilityYou 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.
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.
<p>
To get an idea, try this command on the console:
</p>
<div>
> echo $PATH</p>
<p>
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin</div>
<p>
</p>
<p>
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:
</p>
<div>
> export PATH=$PATH:/usr/local/myscripts/really/great/work/bin
</div>
<p>
Done, now see if Linux can find your script or binary.
</p>
<div>
> which myutility</p>
<p>
/usr/local/myscripts/really/great/work/bin/myutility</div>
<p>
Success! Now test your script.
</p>
<div>
> myutility
</div>
<p>
</p>
<blockquote>
<p>
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.
</p>
</blockquote>
<p>
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.
</p>
<div>
> vi ~/.bash_profile</p>
<p>
# .bash_profile
</p>
<p>
# Get the aliases and functions<br />if [ -f ~/.bashrc ]; then<br />. ~/.bashrc<br />fi
</p>
<p>
# User specific environment and startup programs
</p>
<p>
PATH=$PATH:$HOME/bin
</p>
<p>
export PATH<br />unset USERNAME</div>
<p>
</p>
<blockquote>
<p>
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
</p>
</blockquote>
<p>
Same as what you did earlier, append your script folder to the PATH construct, as shown below
</p>
<div>
PATH=$PATH:$HOME/bin:/usr/local/myscripts/really/great/work/bin
</div>
<p>
Now your done, the next time you login, the new “PATH” should kick in.
</p>
<p>
Congratulations! Now go play with your scripts!
</p>
<p>
<a href="//dl.getdropbox.com/u/1510515/potatokorner/potatokorner-tutorial-export-command.pdf" target="_blank">Download a PDF copy</a>
</p>