All of the command line tools are binary files located in the filesystem. ls command, for example, actually lives in the directory /bin. But how does the shell know to find /bin/ls when you type in at the prompt?
The shell looks in all the directories listed in its built in PATH variable, in search for file that has the same name as the command you'r after. The PATH environment variable for default Ubuntu installation will look something like this:
$ echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
So when you type a command the shell will first look at /opt/local/bin, is it there, no. Is it in /opt/local/sbin, no. and so forth untill it finds the binary file that matches the command.
In the article prior to this one, we made the script counter.sh. When we wanted to execute it we had to ./counter.sh. If that file would be in one of the PATH directories could just write counter.sh to run the script.
Built-in variables such as PATH are known as environment variables. These are the variables that describe the environment in which a processs is running. Examples of other environment variables HOME, which contains the path of the user's home dir, and MAIL, which is the name of the file that stores the user's email. Environment variables work just like normal shell variables, exepr they can be accessed by programs that are launched from the shell too.
To modify an environment variable, you use the export command:
$ export PATH=$PATH:/home/user_name
$ echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/home/user_name
We should now be able to run our scripts that we save in our home dir for the current session. However once you exit the shell its lost. Later in our guide we will have a look at how to save its default PATH variable whenever a shell is launched.
No comments:
Post a Comment