Practice Exams:

LPI 102-500 – 103.1: Working on the command line

  1. History, Manpages and echo

We have arrived at the third global topic, namely topic 103 GNO and Unix commands. There are a few important commands that we need to discuss. First of all, there are three of the commands listed by LPI that we have already met. There would be the commands PWD, you name and man. To repeat the command PWD is in long form print working directory. This command shows us where we now are in the directory tree PWD and we are in home manner. So in my personal home directory now the second command is you name with which we can display various information about the system, for example operating system information and so on. So for example you name with the option A. This shows us everything that you name can show us. And the third already known command is man, which the corresponding man page of a Linux command tells us for example mandme.

 And here we have the man page of D message. First of all, it is important to know where the man pages are. To repeat that would be as follows ll user share men and the individual man pages are located in these subfolders. There are still various hidden man pages for some commands, but these are mainly intended for developers. To access these hidden man pages we would need to install the Manpages dev package. So we should install it with pseudoaptinstall manages dev and in my case it is already installed. Here I have prepared a small overview and you can see the different types of map pages that can be called up. Each type of manage has a specific purpose number one contains executable programs or shell commands number two calls kernel function number three library calls functions in program libraries. Number four is special files, usually in dev. Number five is file formats and conventions and number six is games. Number seven covers different items number eight commands for system administration and number nine the kernel routines.

So in principle you can call up various man pages if they are available, you can call up the different man pages with a corresponding number. For example man one and then chmod. And this man page looks familiar because it is the man page that is output when we call man without number. So you can see the normal man page here with the name, with the description, with the options and so on. So let’s try man two chmod and as you can see here, it looks a little bit different. As admins we are not interested in this type of main page unless we can program ourselves and do something with the content. Let’s see here programming related stuff man three chmod there is no entry in this section man four chmod there is also none. And so you can work your way through the various man pages that are available. Next let’s look at the history. Everything we do in the shell is saved and you can always look again afterwards with the History command.

You can see the last entries history. And I have a whole list of entries here that I made. At some point. Every command that is executed is also saved here. To find out in which file these entries are saved, look at the variable host file. To do this, we would use the command echo. Let’s take a look at the man page beforehand. So man echo. So we have here echo display a line of text. So echo outputs a text. If you want to read out the value of a variable, in this case the variable with the name his file, you always use echo and not cat. In addition, the variable is always capitalized and preceded by a dollar sign so that the system knows that it only has to output the content of a variable and not the content of a file. So we have the system variable his file, and we look at the value or its value with echo dollar hist file, and the result is home manual and then Bash history, which as is, as I said, in my personal home directory. So every user has this history file in his own personal home directory. The period here in front of the file name means that the file is hidden. Now I list the contents with LS. And here we can see that the file doc Bash history is not here. We can make the corresponding file visible if we use LS for the option A, because the option A also shows hidden files.

 And here you see the dot bash history. I prefer the following LS and then La. And we have the long form here. And here we have our dot bash underscore history. We now look at the contents of the Bash history file. And here we have our whole history. Two things are different here when compared to the History command. With History, we still have line markers on the left side. And the last command here shut down h now is a command that I executed yesterday evening, which means that everything is only permanently saved in this file when you shut down the system. To repeat the system variable his file refers to the file bash underscore history, which in turn saves the commands.

If I were to apply cats to the variable, the path to Bash history would not be displayed. But the contents of Bash history again to differentiate echo dollar his file. And we get that. As a result, if we use cat on his file, then only the file dot bash underscore history is output. So let’s get back to the subject of echo. With echo, you can output variables and character strings on the screen. You can also output your own direct output. You can put the corresponding text in double or single quotation marks. As an example, echo test, test or echo test, test or echo test.

The result is the same for all of them. Various commands options can be used with the e option. For example, echo and then test n is an option that will convert it to a line break. So this one here, n, is a line break and this line break only breaks only if you specify the e option. If you omit this, it is written out normally. We could try that echo test. Another example could be echoed and then test t test a tab should now be inserted here or is now inserted here. For a list of other options, see the main page man echo and then you will see other options here for backslash alert, backspace escape form, feet, new line, horizontal tab, vertical tab and so on and so on.

  1. bash, env, set, unset, export

Before really getting into this topic, just a reminder what is the Linux shell? A shell is the command line environment with which you work on your Linux system. There are different shells with different functions and different variables. The standard shell is the so called Bash. Bash is the abbreviation for born again shell. There are various others, for example sh, CSH or Zshsh. For the epic exam we only need knowledge and experience about Bash. So let’s take a closer look at the Bash environment. Bash has various so called environment variables. These environment variables contain settings for how the Bash environment is configured. With the command NF for environment you can display the current environment variables and their values. And here we have a whole lot of fixed variables. Of course we don’t need to know all of these variables, but I think many of them also explain themselves. For example we have here the username variable and the value is my username. We have the home variable, the value is my home directory home manual.

We have the PWD variable and it is also home manual. The PVD variable is responsible for our start after the login. So when I log into the system and I start the terminal, then I am in this directory. Here I’m located in this directory if I would change the PWD variable to home manual test, then after the login I would find myself in home manual test. We have here for example the shell variable with the value bin Bash. So every time we are starting our terminal it starts the Bash. Here we have the Lang variable. So the language in our system it is US English here. Here we have the LC underscore time variable, that is the time format and it is shown in German. Here in my case and here very important is the path variable. It shows the paths in which the system searches for executable programs.

For example if I type init the command in it then it would normally not work because in it is not in the directory I am currently in in it is stored in the directory bin and at the moment I am in home manual. So when I say in it, normally it shouldn’t work because the init program is not stored in home manual. So normally we should write down SBIN in it. But in this case the path variable you see here different paths. We have here user local aspin user local bin user espin user bin here we have Aspin. Here we have bin and others. So every program that is stored in Aspin we can call the program without writing down the whole path. So we just could use in it. Because the Aspin folder is stored in the path variable. It is important to know and important to understand.

I hope you understood my explanation. Maybe another example. If we want to install a program in the folder opt and we want to start it via the command line we simply change the path variable and simply add opt here at the end and then we could start the program with the command from everywhere without using the whole path. Of course, AMF always shows all the variables that Bash has to offer. If we know various variables by heart, we can also display them with the echo command. To reiterate, if I want to look at the path variable I would choose echo dollar and then path and then I get the corresponding value displayed.

Maybe another example echo dollar shell and it shows us the value of shell of the variable shell and the value is bin Bash. Or another one we could use echo dollar home and it shows us the variable Home and it is Home Manual. The Set command also shows shell variables but there are other variables and the so called shell setting to Set. And yeah, the output is very large here we have to scroll up to the top because here that is programming language. We don’t need to know that, don’t need to use that. But here are the interesting things for us. We have different more variables. For example the Hist file, it is Home manual and then Bash underscore history and this file is our history of our commands stored. So every command I have shown you in this and in the last videos is stored in the Bash history file. And if tomorrow I want to know what I have done today, then I could look in homemonal Bash history file and I could see which commands I have entered to the Bash.

Here we have the Hist file size, we could change that to 3000, 5000, what else? Hist size thousand commands are stored now and we could set that up or down if we want. Here we have some other variables. Again a home variable, we have a host name variable, a host type variable, some language variables here, very important, we come back to this in a later video and so on. Set can also be used to set your own variables or to change settings on the shell. We can use the shell for example to switch in the Debug mode. Debug Mode means that the shell gives out everything it has. So let’s switch to the Debug mode with Set minus x and with that the Debug mode is switched on. I have written a small script, it’s just a line of text is written in the file that should be output and let’s start the script. The script name is Tests Shy and the information in the first line indicates that the script has started.

And if we weren’t in Debug Mode we wouldn’t see that. Usually the Debug Mode is intended so that if you write larger scripts but they do not work because of an undetectable error, it may then be able to provide us with information that we would not get that way and that could lead to a solution to the problem. To switch off the debug mode again, we use the command set plus x. If I run the script again now we will no longer see this information here. In the first line we have just our output, as it should be. The opposite pole to set is the unset command, with which variables and functions can be removed. I create, for example, new variable with variable equals test 1815. When we create a variable, we choose the variable name in uppercase and assign the value tests 815 to this variable. We can check that with echo dollar variable 1815 and yeah, we see here the content of the variable test 815.

That means the creation of the variable worked to delete this variable again we select the command unset and then variable one at 15. And if we now again use echo, there is no longer any value here. I just created a variable with a variable name variable zero 815 and the value test zero 815. This variable is only valid in this Bash session. We will check that briefly. So I will create the variable again, because we just deleted it. I can now with the command Bash start a second Bash, even if you haven’t seen any change. Now this is called sub Bash and is a switching process. So a Bash that goes away from the first Bash. And if we are here now, let me just enter echo variable zero 815, then we have no result. This means that the variable was not passed to the new shell. If I exit the shell with exit, I am back in the old shell. And if I am now using echo dollar variable zero 815, the variable is there again, or the value in the variable is there again. With the export command I can create a variable that can also be transferred to other nested shells.

I delete the variable again with unset variable zero 815 and created again with the export command. So export variable equals test echo variable. It works. Now we start a sub Bash sub shell with Bash and now we try it again with echo variable zero 815 and we see the variable process also taken over. This only works when we use the export command. We can also create a variable without an export command, but this is then only valid in this Bash session. It also only works with the switching processes than with parade processes, if the variable has been specified. If I now open a completely new terminal window and we have a completely new Bash, that has nothing to do with the old one new window here, and we try it here with echo dollar variable 15, there is no result here either. This means that the variable able is not tasked globally in every corresponding shell, but only to the corresponding sub processes.