BOSS-Linux/C2/The-Linux-Environment/English-timed

From Script | Spoken-Tutorial
Revision as of 17:08, 12 August 2015 by Sandhya.np14 (Talk | contribs)

Jump to: navigation, search
Time Narration
00:01 Welcome to this spoken tutorial on the Linux environment and ways to manipulate it.
00:07 A working Linux system will be required to try out the examples illustrated in this tutorial.
00:12 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.
00:20 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.
00:30 Please note that Linux is case sensitive and all the commands used in this tutorial are in lower case unless otherwise mentioned.
00:40 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.
00:48 Linux can be highly customized by changing the settings of the shell.
00:51 Let us understand how all this can be done.
00:54 The behavior of the shell is generally determined by the shell variables.
00:58 There are mainly two kinds of shell variables: Environment Variables and Local Variables.
01:06 Environment variables, named so because they are available entirely in the user's total environment.
01:12 These are also available in sub-shells spawned by the shell like the ones for running shell scripts.
01:18 Local Variables which as the name suggests have a more restricted or limited availability.
01:24 These are not available in the sub-shells spawned by the shell.
01:28 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.
01:41 To see all the variables available in the current shell, we run the command 'set'.
01:46 Type at the terminal: set space pipeline character more and press Enter.
01:53 We can see all the current shell variables. ,
01:58 For example- take a look at the HOME environment variable; also notice the value assigned to it.
02:07 Press Enter to move through the list and in order to come out, press q.
02:14 Here, the output from set was pipe-lined to more in order to display a more systematic multi-page output of the variable list.
02:31 To see only the environment variables, run the command 'env'.
02:38 Type at the terminal: env space 'vertical-bar' more and press Enter.
02:46 For example, notice the SHELL variable whose value is slash bin slash bash.
02:53 Again, you may press q to come out of the list.
03:00 Now, let us discuss some of the more important environment variables in Linux.
03:05 We would be using bash shell for all our demonstrations here.
03:09 Different shells are customized in slightly different ways.
03:12 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.
03:24 The first environment variable that we would see is the SHELL variable.
03:28 It stores the name of the current shell.
03:31 To see what is the value of the SHELL variable, type at the terminal: echo space dollar S-H-E-L-L in capitals and press Enter.
03:48 Here, slash bin slash bash is the shell where we are currently operating.
03:56 The next variable is HOME.
03:58 When we login into Linux, it normally places us in a directory named after our user name.
04:04 This directory is called the home directory and this is exactly what is available in the 'HOME' variable.
04:10 To see the value, type at the terminal: echo space dollar H-O-M-E in capital and press Enter.
04:22 The next environment variable is PATH.
04:26 The PATH variable contains the absolute paths of the directories that the shell is supposed to search for locating any executable command.
04:33 Let's see the value of the path variable.
04:36 Again, type at the terminal: echo space dollar P-A-T-H in capitals and press Enter.
04:45 On my computer, it shows slash user slash local slash bin slash user slash bin etc.
04:57 This may slightly vary from one system to another.
05:00 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.
05:11 We can also add our own directory to this list so that our directory is also searched by the shell.
05:18 In order to add our own directory, type at the terminal:
05:22 P-A-T-H in capitals 'equal-to' dollar P-A-T-H again in capital colon slash home slash the name of my own home directory and press Enter.
05:47 Now, if we echo the value of PATH,
05:57 our added directory will also be a part of the PATH variable.
06:03 See, the directory is now present here.
06:09 Another interesting variable is the LOGNAME.
06:12 It stores the username of the currently active user.
06:16 In order to see the value, type: echo space dollar LOGNAME and press Enter.
06:28 When we open the terminal, we can see the dollar sign which is the prompt at which we enter all our commands.
06:35 This is the primary prompt string represented by the environment variable 'PS1'.
06:40 There is a secondary prompt string also.
06:43 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.
06:53 This is the secondary prompt string represented by the environment variable 'PS2'.
06:58 To see the value of the 'secondary command prompt', type at the terminal: echo space dollar PS2 and press Enter.
07:13 We may change our 'primary prompt string' to say “at the rate” (@) at the prompt.
07:20 In order to get this done, type: PS1 'equal-to' now within quotes 'at the rate(@)' “ and press Enter.
07:34 Now, instead of the dollar sign we can see the at the rate sign as the prompt.
07:43 We may do something more interesting like- we may display our username at the prompt.
07:49 Just type: PS1 in capital 'equal-to' within quotes dollar LOGNAME and press Enter.
08:05 Now, my username is my prompt.
08:09 To revert back, type: PS1 'equal-to' dollar ($) within quotes and press Enter.
08:21 We have assigned values to many of the environment variables.
08:25 But, remember one thing that these modifications are only applicable for the current session
08:30 like we had just added our directory to the PATH variable.
08:34 If we close the terminal and open it again or open a new terminal altogether and check the 'path' variable by echoing its value,
08:53 we will be surprised to see that our modifications are no longer present.
08:59 The way by which we can make these modifications permanent will be covered in some advanced tutorial.
09:06 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?
09:15 No, there are a number of solutions.
09:19 First, normally if you press the up-key on your keyboard then it will show the last command that you have typed.
09:26 Keep pressing it and it will keep scrolling through the previous commands.
09:30 To go back, press the down-key.
09:36 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.
09:45 Type at the prompt history
09:51 and press Enter; see a list of previously executed commands appears.
09:57 If instead of the large list, you wanted to see only the last ten,
10:02 type: history space 10' and press Enter.
10:13 Notice, in this list, there is a number assigned to each of the previously executed commands.
10:20 In order to repeat a particular command,
10:24 just type an exclamation mark followed by the number of the command. For example- 442 in my case, would execute echo space dollar PATH.
10:44 If you need to re-execute the last command, simply type exclamation mark twice and press Enter.
10:56 The next thing we would see is called tilde substitution. The tilde(~) character is a shorthand for the home directory.
11:05 So, say you have a directory with the name 'testtree' in your home directory. You can move to it by typing: cd space ~(tilde) slash testtree.
11:18 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

11:28 Like now that we are in the testtree directory, the last directory we visited was the home directory.
11:34 So, if we run cd space minus and press Enter, it will go to the home directory.
11:40 Run it again and it will take us back to the testtree directory.
11:47 The last but quite important command we will see is the alias command.
11:51 It may quite happen that you have a large command that needs to be run again and again.
11:57 In this case, we can give it a short alias name and use the alias name instead to invoke it.
12:03 Assuming that you have such a long directory hierarchy that you frequently visit for music, you may create an alias for it like this.
12:13 Type: alias space cdMusic 'equal-to' within double quotes cd space slash home slash the name of my own home directory slash music and press Enter.
12:39 Now, every time you need to switch to this directory, simply write cdMusic and press Enter.
12:47 See, we are in the music directory now.
12:52 Now, you may type cd space minus

and the prompt to go back to the previous working directory.

13:01 To unset an alias, simply write unalias space cdMusic and press Enter.
13:13 Now again if you fire cdMusic from the terminal, you will get an error stating that the command was not found.
13:22 Suppose, we have two files- test1 and test2 in our present working directory
13:31 and if we fire rm test1, test1 is silently deleted.
13:38 We know that hyphen i option of the rm command makes the removal process interactive.
13:45 So, we may set an alias like, alias rm equal-to, now within quotes rm space hyphen i.
13:56 Now, when we run rm, rm hyphen i will actually be run.
14:05 So, we saw that while test1 was silently deleted, system asked before deleting test2.
14:13 So, in this tutorial, we have learned about environment variables, history and aliasing.
14:18 This brings me to the end of this tutorial.
14:21 Spoken Tutorials are a part of the Talk to a Teacher project, supported by the National Mission on Education through ICT.
14:29 More information on the same is available from our website.
14:32 The Script for this tutorial was created by Anirban.
14:36 This Anuvrath Parashar from MIT university, signing off.

Contributors and Content Editors

Gaurav, PoojaMoolya, Pratik kamble, Sandhya.np14