From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: The Linux Environment

Author : Anirban Roy Choudhury

Keywords: Environment variables, local variables, PATH,HOME, profile, history, alias

Resources for the tutorial:

Find the HOME variable from the list and hover mouse over variable and the value after the '=' sign respectively] point the mouse to SHELL=/bin/bash ] create a directory named testtree in the home directory
Visual Cue
Display Slide 1 Welcome to this spoken tutorial on the Linux environment and ways to manupulate it.
Display Slide 2

A working linux system, preferably Ubuntu will be required to try out the examples illsutrated 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.
Display Slide 3 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.
Slide 3 (continued)

The behaviour of the shell is generally determined by the shell variables.
There are two main kinds of shell variables:

Environment Variables

Local Variables

Slide 4

Environment variables, named so because they are available entirely in the user's total environment. These are also available in subshells spawned by the shell like for running shell scripts.
Local Variables , which as the name suggests have a more restricted or limited availability. These are not available to the subshells created by the shell.
While in this tutorial, we will mainly talk about environment variables, let us first see how the value of these variables can be seen.
Open a terminal

Type at the command prompt

"set | more"

press enter

To see all the variables available in the current shell , we run the command set. Type at the terminal

"set 'vertical-bar' 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

press q

press Enter to move through the list , to come out press q

Use up arrow key to move back through history and highlight the pipeline character Here the output from set was pipelined to more in order to show a more systematic multipage output of the variable list.

->Environment Variables

To see only the environment variables run the command env
Type at the command prompt

"env | more"

press enter

Type at the terminal

"env 'vertical-bar' more"

For example,

Notice the SHELL variable whose value is /bin/bash .

press q 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.
Type at the command prompt

"echo $SHELL"

press enter

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 SHELL in capital"and press enter.

Here /bin/bash is the shell where we are currently operating.
Type at the command prompt

"echo $HOME"

press enter

The next variable is the 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 HOME in capital"and press enter
-The Path Variable The next environment variable is the PATH.
Type at the command prompt

"echo $PATH"

press enter

The PATH variable contains the absolute paths of the directories that the shell is supposed to search for locating any executabe command.

Lets see the value of the path variable.

Again, type at the terminal "echo space dollar PATH [in capitals]"

On my computer it shows the directories

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin etc.

This may slightly vary from one system to other.
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 add our own directory to this list so that our directory is also searched by the shell.
Type at the command prompt


press enter

In order to add our own directory type at the terminal "PATH [in capital] 'equal-to' dollar PATH again [in capital] colon /home/ the_name_of_my_own_home_directory” and press enter.
Type at the command prompt

"echo $PATH"

press enter

Now if we echo the value of PATH,
our added directory will also be a part of the PATH variable.
select and hover over the change in value of path variable See the directory is now present here.
Type at the command prompt

"echo $LOGNAME"

press enter

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.
Type at the command prompt

" echo $PS2 "

press enter

press enter a few times to highlight the change

When we open the terminal we can see the dollar sign, which is the prompt at which we enter all our commands.
Type at the command prompt

" PS1="@" "

press enter

This is the primary prompt string represented by the environment variable PS1.
press enter few times to highlight the change

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 secondary command prompt, type at the terminal "echo space dollar PS2 [in capital]" and press enter.
We may change our primary prompt string to say “at the rate” <@> at the prompt.
Type at the command prompt


press enter

In order to get this done

Type "PS1 [in capital] 'equal-to' now withing quotes 'at the rate' “ and press enter.

Now instead of the dollar sign we see an at the rate sign.
We may do something more interesting. Like we may display our username as the prompt.
Type at the command prompt


press enter

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."
Open another tab in the terminal ctrl+shift+t We have assigned values to many of the environment variables.
But, remember one thing that these modifications are only applicable for the current session.
Type at the command prompt

"echo $PATH"

press enter

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 echo-ing its value
we will be surprised to see that our modificaitons are no longer present.
The way by which we can make these modifications permanent will be covered in some advanced tutorial.
Display slide 4

Often we want to re-execute a command 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.
Press up direction key First, normally if you press the up direction key in your keyboard then it will show the last command that you typed.
Keep pressing and it will keep scrolling through the previous commands.
Press down direction key To go back press the down direction 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 command prompt


Type at the prompt "history"
press enter and press enter, see a list of previously executed commands appears
If instead of the large list you want to see, only the last ten
Type at the command prompt

"history 10"

press enter

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.

Take a random example

Type at the command prompt


type any number that you are getting in the list given by "history 10" command

press enter

Just type exclamation mark followed by the number of the command for example 442 in my case would execute echo space dollar path
Type at the command prompt


press enter

If you need to execute the last command simply type exclamation mark twice and press enter.
Tilde character Substitution The next thing we would see is called tilde substitution

The tilde(~) charactar 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".

Type at the command prompt

"cd ~/testtree "

press enter

Run pwd to illustrate the change

One may also toggle between the current working directory and the last directory used by giving the command

cd ~- [narration - cd space 'tilde' minus] or only cd - [ narration - cd space minus]

Type at the command prompt

"cd -"

press enter

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" . It will go to the home directory.

Press the up key and enter to re-execute the previous command.

Run it again and you are back to the testtree directory.
(Before the video create such a long directory hierarchy)

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 at the command prompt

" alias cdMusic = "cd /home/arc/files/entertainment/music" "

press enter

Type " alias space cdMusic 'equal-to' within double quotoes cd space /home/arc/files/entertainment/music " and press enter
Type at the command prompt


press enter

Quickly run pwd to illustrate the effect of the previous command

Now every time you need to switch to this directory simply write cdMusic and press enter. See, we are in the music directory now.
Type at the command prompt

"cd -"

Now, you may type at the prompt "cd space minus" to go back to the previous working directory.
Type at the command prompt

"unalias cdMUsic"

press enter

To unset this alias simply write unalias space cdMusic
Type at the command prompt


press enter

Now again if you fire cdMusic from the terminal, you will get an error stating that the command was not found.
quickly run

touch test1 test2

to create test1 and test2 in the present working directory. Then run ls illustrate the change.

Suppose we have two files, test1 and test2 in our present working directory
Type at the command prompt

"rm test1"

press enter

and if we fire rm test1, test1 is silently deleted.
We know that the “hyphen i” option of the rm command can make the removal process interactive.
Type at the command prompt

" alias rm ="rm -i" "

press enter

So we may set an alias like, alias rm equal-to, now within quotes “rm space -i”
Type at the command prompt

"rm test2"

press enter

Now when we run “rm” ,” rm-i” will actually be run.\
So we saw that while test1 was silently deleted, system asked before deleting test2.
Display Slide 5 So, in this tutorial, you have learned about environment variables, history and aliasing.
Display Slide 6 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

And this is Anuvrat Parashar from Amity University signing off. Thanks for joining.

Contributors and Content Editors

Chandrika, Pravin1389