< Previous | Contents | Next >
Script File Location
With the permissions set, we can now execute our script:
[me@linuxbox ~]$ ./hello_world
Hello World!
[me@linuxbox ~]$ ./hello_world
Hello World!
In order for the script to run, we must precede the script name with an explicit path. If we don’t, we get this:
[me@linuxbox ~]$ hello_world
bash: hello_world: command not found
[me@linuxbox ~]$ hello_world
bash: hello_world: command not found
Why is this? What makes our script different from other programs? As it turns out, noth- ing. Our script is fine. Its location is the problem. Back in Chapter 11, we discussed the PATH environment variable and its effect on how the system searches for executable pro- grams. To recap, the system searches a list of directories each time it needs to find an exe- cutable program, if no explicit path is specified. This is how the system knows to execute
/bin/ls when we type ls at the command line. The /bin directory is one of the di- rectories that the system automatically searches. The list of directories is held within an environment variable named PATH. The PATH variable contains a colon-separated list of directories to be searched. We can view the contents of PATH:
Script File Location
[me@linuxbox ~]$ echo $PATH
/home/me/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:
/bin:/usr/games
[me@linuxbox ~]$ echo $PATH
/home/me/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:
/bin:/usr/games
Here we see our list of directories. If our script were located in any of the directories in the list, our problem would be solved. Notice the first directory in the list,
/home/me/bin. Most Linux distributions configure the PATH variable to contain a bin directory in the user’s home directory, to allow users to execute their own programs. So if we create the bin directory and place our script within it, it should start to work like other programs:
[me@linuxbox ~]$ mkdir bin [me@linuxbox ~]$ mv hello_world bin [me@linuxbox ~]$ hello_world
Hello World!
[me@linuxbox ~]$ mkdir bin [me@linuxbox ~]$ mv hello_world bin [me@linuxbox ~]$ hello_world
Hello World!
And so it does.
If the PATH variable does not contain the directory, we can easily add it by including this line in our .bashrc file:
export PATH=~/bin:"$PATH"
export PATH=~/bin:"$PATH"
After this change is made, it will take effect in each new terminal session. To apply the change to the current terminal session, we must have the shell re-read the .bashrc file. This can be done by “sourcing” it:
[me@linuxbox ~]$ . .bashrc
[me@linuxbox ~]$ . .bashrc
The dot (.) command is a synonym for the source command, a shell builtin which reads a specified file of shell commands and treats it like input from the keyboard.
Note: Ubuntu (and most other Debian-based distributions) automatically adds the
~/bin directory to the PATH variable if the ~/bin directory exists when the user’s .bashrc file is executed. So, on Ubuntu systems, if we create the ~/bin directory and then log out and log in again, everything works.