Envirionment and Path:
Part 1: making an executable shell script
- Create a local variable hey (the greater_than symbol represents your shell prompt):
-
> hey=hello > echo $hey hello
- Now make your first shell script:
-
Put the line
echo $heyinto a file. Save the file ashey_there, and change the permissions onhey_thereto make it executable:> chmod 755 hey_there
Now run this file:>./hey_there >
- What happened?
- The variable
$heyis a local variable to the shell process. When it spawned the processhey_therethe variable$heywas not passed down to this process. So there is no value for$heyin the subprocess. Tryenvyou will see thatheydoes not show up. - Now make a $hey a global variable:
-
> export hey=hello
- and run the command:
-
>./hey_there hello >
Check the environmentenv. Do you see the variable show up?
Part 2: making your own bin directory
- The first part is not so hard
-
From your home directory make the directory:
> mkdir bin
- Now the tricky part add the directory to your PATH:
-
> export PATH="/home/you/bin:$PATH"
- What's going on?
- You are exporting the variable
PATH(remember the first part of the homework?) to equal your new directory plus the old value of the variable$PATH. Remember the$PATHis expanded by the shell *before* the arguements are passed toexportso this works. - Don't get confused
- When declaring the shell variable you don't use the
$. but when you want to use the value of a shell variable you do. - Making the change in
PATHpermanent - The astute reader has noticed that the change in
PATHjust decsribed will only last for this session with the shell. Inorder to make this change permanent, the lineexport PATH="/home/you/bin:$PATH"should be called for each shell upon log-in. Unfortuantely there is some difference about how the distributions handle this. Unsually these files are in your home directory and called either .bashrc, or .bash_profile. .profile is sometimes a file which is called by any shell which is not good for this command becasueexportis a bash built-in command (trywhich export). On my Redhat system .bash_profile calls .bashrc which calls /etc/bashrc (for system wide configuration stuff). Poke you nose in these files (less,cat?) and try to figure out which one is used in your distribution. There may be information pertaining to this in your distribution manuals. If there aren't any files. I would add the line to a file called .bashrc and log in again to see if thePATHwas changed.
Copyright Marco Scoffier, released under the GFDL