Welcome to this spoken tutorial on The Linux Environment and ways to manipulate it.
A working Linux system, preferably Ubuntu will be required to try out the examples illustrated in this tutorial.
We assume that you know how to get started with the Linux operating system and have some basic idea about commands, file systems and shell.
If you are interested or need to brush these concepts up, please feel free to do so through another spoken tutorial available on our website.
Please note that Ubuntu 10.10 was used for recording this tutorial.
Please also note that Linux is case sensitive and all the commands used in this tutorial are in lower case unless otherwise mentioned.
The Linux environment determines how the operating system behaves with you, how it responds to your commands, how it interprets your actions and so on.
Linux can be highly customized by changing the settings of the shell.
Let us understand how all this can be done.The behavior of the shell is generally determined by the shell variables.
There are mainly two kinds of shell variables: Environment Variables and Local Variables.
Environment variables, named so because they are available entirely in the user's total environment.
These are also available in sub-shells spawned by the shell like the ones for running shell scripts.
Local Variables, which as the name suggests, have a more restricted or limited availability.
These are not available in the sub-shells spawned by the shell.
While in this tutorial, we will mainly talk about environment variables. Let us first see how the value of these shell variables can be seen.
To see all the variables available in the current shell, we run the command set.
Type at the terminal: set space pipeline character more and press Enter.
We can see all the current shell variables.
For example: take a look at the HOME environment variable, also notice the value assigned to it.
Press Enter to move through the list and in order to come out press q.
Here, the output from set was pipelined to more in order to display a more systematic multipage output of the variable list.
To see only the environment variables, run the command env.
Type at the terminal:env space 'vertical-bar' more and press Enter.
For example, notice the SHELL variable whose value is slash bin slash bash.
Again, you may press q to come out of the list.
Now let us discuss some of the more important environment variables in Linux.
We would be using the bash shell for all our demonstrations here.
Different shells are customized in slightly different ways.
To see what a variable actually stores, we have to prefix a dollar sign to the name of that variable and use the echo command along with it.
The first environment variable that we would see is the SHELL variable.
It stores the name of the current shell.
To see what is the value of the SHELL variable, type at the terminal: echo space dollar S-H-E-L-L in capital and press Enter.
Here, slash bin slash bash is the shell where we are currently operating.
The next variable is HOME.
When we login into Linux, it normally places us in a directory named after our user name.
This directory is called the 'home directory' and this is exactly what is available in 'HOME' variable.
To see the value, type at the terminal: echo space dollar H-O-M-E in capital and press Enter.
The next environment variable is PATH.
The PATH variable contains the absolute paths of the directories that the shell is supposed to search for locating any executable command.
Let's see the value of the path variable.
Again, type at the terminal: echo space dollar P-A-T-H in capitals and press Enter.
On my computer it shows slash user slash local slash sbin slash user slash local slash bin slash user slash sbin slash user slash bin etc.
This may slightly vary from one system to another.
It is actually a list of directories separated by the :(colon) delimiter, that the shell would search in this order for finding an executable command.
We can also add our own directory to this list so that our directory is also searched by the shell.
In order to add our own directory type at the terminal:
P-A-T-H in capital 'equal-to' dollar P-A-T-H again in capital colon slash home slash the name of my own home directory and press Enter.
Now if we echo the value of PATH,
our added directory will also be a part of the PATH variable.
See the directory is now present here.
Another interesting variable is the LOGNAME.
It stores the username of the currently active user.
In order to see the value, type: echo space dollar LOGNAME and press Enter.
When we open the terminal we can see the dollar sign which is the prompt at which we enter all our commands.
This is the primary prompt string represented by the environment variable 'PS1'.
There is a secondary prompt string also.
If our command is long and it spans for more than one line then from the second line onwards we can see a greater than sign “>” as the prompt.
This is the secondary prompt string represented by the environment variable 'PS2'.
To see the value of the secondary command prompt, type at the terminal: echo space dollar PS2 and press Enter.
We may change our primary prompt string to say “at the rate” <@> at the prompt.
In order to get this done, type PS1 'equal-to' now within quotes 'at the rate' “@"and press Enter.
Now instead of the dollar sign we can see the at the rate sign as the prompt.
We may do something more interesting. Like we may display our username at the prompt.
Just type: PS1 in capital 'equal-to' within quotes dollar LOGNAME and press Enter.
Now my username is my prompt.
To revert back, type: PS1 'equal-to' dollar within quotes and press Enter.
We have assigned values to many of the environment variables.
But, remember one thing that these modifications are only applicable for the current session.
Like we had just added our directory to the PATH variable.
If we close the terminal and open it again or open a new terminal altogether and check the path variable by echoing its value,
we will be surprised to see that our modifications are no longer present.
The way by which we can make these modifications permanent will be covered in some advanced tutorial.
Often we want to re-execute a command that we had executed in the recent past. What do we do? Do we have to type the entire command again?
No, there are a number of solutions.
First, normally if you press the 'up key' on your keyboard then it will show the last command that you typed.
Keep pressing and it will keep scrolling through the previous commands.
To go back press the down key.
But when you have to scroll through many commands this becomes a little clumsy and tedious. A better way is to use the history command.
Type at the prompt history.
and press Enter, see a list of previously executed commands appear.
If instead of the large list you want to see only the last ten,
type history space 10' and press Enter.
Notice, in this list there is a number assigned to each of the previously executed commands.
In order to repeat a particular command,
just type exclamation mark followed by the number of the command for example 442 in my case would execute echo space dollar PATH
If you need to re-execute the last command simply type exclamation mark twice and press Enter.
The next thing we would see is called 'tilde' substitution. The tilde(~) character is a shorthand for the home directory.
So, say you have a directory with name testtree in your home directory. You can move to it by typing: cd space ~(tilde) slash testtree.
One may also toggle between the current working directory and the last directory used by giving the command cd '~(tilde)' minus or only cd minus
Like now that we are in the testtree directory, the last directory we visited was the home directory.
So if we run cd space minus and press Enter, it will go to the home directory.
Run it again and it will take us back to the testtree directory.
The last but quite important command we will see is the alias command.
It may happen that you have a large command that needs to be run again and again.
In this case we can give it a short alias name and use the alias name instead, to invoke it.
Assuming that you have such a long directory hierarchy that you frequently visit for music, you may create an alias for it like this.
Type: alias space cdMusic 'equal-to' within double quotes cd space slash home slash arc slash files slash entertainment slash music and press Enter.
Now every time you need to switch to this directory simply write cdMusic and press Enter.
See, we are in the music directory now.
Now, you may type cd space minus at the prompt to go back to the previous working directory.
To unset an alias, simply write unalias space cdMusic and press Enter.
Now again if you fire cdMusic from the terminal, you will get an error stating that the command was not found.
Suppose we have two files, test1 and test2 in our present working directory
and if we fire rm test1, test1 is silently deleted.
We know that hyphen i option of the rm command makes the removal process interactive.
So we may set an alias like, alias rm equal-to, now within quotes rm space hyphen i
Now when we run rm ,rm hyphen iwill actually be run.
So we saw that while test1 was silently deleted, system asked before deleting test2.
So, in this tutorial, you have learned about environment variables, history and aliasing.
This brings me to the end of this tutorial.
Spoken Tutorials are a part of the Talk to a Teacher project, supported by the National Mission on Education through ICT.
More information on the same is available from our website.
The Script for this tutorial was created by Anirban.
This script has been contributed by ----------------------(name of the translator) and this is -----------------------(name of the recorder) from --------------------------(name of the place)signing off.