Ict-innovation/LPI/103.1

= 103.1 Working on the Command Line = ''Candidates should be able to interact with shells and commands using the command line. The Objective assumes the bash shell.''

Key Knowledge Areas


 * Use single shell commands and one line command sequences to perform basic tasks on the command line.
 * Use and modify the shell environment including defining, referencing and exporting environment variables.
 * Use and edit command history.
 * Invoke commands inside and outside the defined path.

Overview
A basic way to interact with a computer system is to use the command line. The shell interprets the instructions typed in at the keyboard. The shell prompt (ending with $ or # for user root) indicates that it is ready for user input.

The shell is also a programming environment which can be used to perform automated tasks. Shell programs are called scripts.

Since the bash shell is one of the most widely used shells in the Linux world the LPI concentrates mainly on this shell.

The interactive shell
Shell commands are often of the form

''' command [options] {arguments}. '''

The bash shell uses the echo command to print text to the screen.

Full/Relative path

The shell interprets the first ¨word¨ of any string given on the command line as a command. If the string is a full or relative path to an executable then the executable is started. If the first word has no ¨/¨ characters, then the shell will scan directories defined in the PATH variable and attempt to run the first command matching the string.

For example if the PATH variable only contains the directories /bin and /usr/bin then the command xeyes won't be found since it is stored in /usr/X11R6/bin/xeyes so the full path needs to be used

An alternative to typing the full path to an executable is to use a relative path. For example, if the user is in the directory where the xeyes program is stored then one can type

The '.' in the above command means the current working directory. Since you are already in the current directory the command could also simply be written as: (only if “.” is in your search path)

Shell Variables
Shell variables are similar to variables used in any computing language. Variable names are limited to alphanumeric characters. For example CREDIT=300 simply assigns the value 300 to the variable named CREDIT.

The value of a variable can be removed with the unset command.

Export, Set and Env

There are two types of variables: local and exported.

Local variables will be accessible only to the current shell. On the other hand, exported variables are accessible by both the shell and any child process started from that shell.

The commands set and env are used to list defined variables

A global variable is global in the sense that any child process can reference it. The example below exports the variable credit and then checks to see if it has been exported as expected.

List of common predefined variables

Special variables

The next few variables are related to process management.

Metacharacters and Quotes

Metacharacters are characters that have special meaning for the shell. They are mainly used for file globbing, that is to match several files or directory names using a minimum of letters. The input (<), output (>) and pipe (|) characters are also special characters as well as the dollar ($) sign used for variables. We will not list them here but note that these characters are seldom used to name regular files.

Wildcards

 * The  *  wildcard can replace any number of characters.

lists all programs starting with a 'b'


 * The ? wildcard replaces any one character.

lists all programs having a 'b' as the second letter


 * [ ] is used to define a range of values.

First line lists all files starting with an 'a' and have a digit in second position.

The second line lists all files that don't start with and 'a' or an 'A'


 * {string1,string2} although not a file naming wildcard, it can be used to generate a list of names that have a common stem.

Quotes and escape codes

The special meaning of metacharacters can be cancelled by escape characters, which are also metacharacters.

The backslash (\) is called the escape character and cancels the meaning of the following character, forcing the shell to interpret it literally.

The single quotes (' ') cancel the meaning of all metacharacters except the backslash.

The double quotes (" ") are the weakest quotes but cancel most of the special meaning of the enclosed characters except the pipe (|), the backslash (\) and a variable ($var).

The Back Tick

Back quotes `` will execute a command enclosed and substitute the output back on the command line. The next example defines the variable TIME using the date command.

Another way of executing commands (similar to the back ticks) is to use $. This will execute the enclosed command and treat it as a variable.

The Command History
To view the list of previously typed commands you can use the bash built-in command history.

This has listed all the cached commands as well as the commands saved in ~/.bash_history. When a user exits the shell cached commands are saved to ~/.bash_history.

You can recall commands by using the Up-arrow and Down-arrow on your keyboard. There are also emacs key bindings that enable you to execute and even edit these lines.

The bang (!) key can be used to rerun a command.

Example:

Other Commands

Aliases

You can create aliases for commands needing many arguments. The format to create an alias is

By typing alias alone at the command line you will get a list of currently defined aliases.

Command completion

By pressing TAB, the shell will complete the commands you have started typing in.

Compound commands

The ''exec” command

This command is not a binary but rather is part of the shell. It is used to start other commands. Ordinarily if a command is executed, a sub-process is started. If the exec command is used to initiate the new program, it reoccupies the process used to start it. It replaces the current shell (in a script or the interactive shell).

When the new command terminates, control is not passed back to the calling shell, but returns to the process that called the shell used to make the exec call.

The above shows control falling back to the second shell (process 455) after a straight forward echo and the first shell (process 414) using an exec.

Manpages and the whatis database
These are the main topic sections one can expect to find in a manpage.

The whatis database stores the NAME section of all the manpages on the system. This is updated regularly through a daily cron. The whatis database has the following two entries:

The syntax for whatis is:

 whatis 

The output is the full NAME section of the manpages where string matched named(key)

One can also use the man command to query the whatis database. The syntax is

 man -k 

This command is similar to apropos. Unlike whatis this will query both the “name” and the “one line description” entries of the database. If the string matches a word in any of these fields the above query will return the full NAME section.

Example: (the matching string has been highlighted)

The filesystem hierachy standard, a recommended layout for Linux filesystems, recommends manpages to be kept in /usr/share/man. However additional locations can be searched using the MANPATH environment variable set in /etc/man.config. Each directory is further divided into subdirectories corresponding to manpage sections.

Sometimes manpages with the same name are present in more than one section.

To access a specific section N one has to enter:

man N command

Examples:

file

file is used to try and detect what type a particular file is.

For example

The utility will identify files that have been incorrectly named so if picture.png had been named readme.txt the command “file readme.txt” would still identify the file as a png file.

uname

The uname command prints information relating to the kernel version, machine name, processor type and node name. It is most commonly used to identify which version of the kernel a machine is running.

Prints the currently running kernel's version number.

pwd

This is a command which simply prints out the current working directory for the shell.

Used files, terms and utilities:* bash
 * echo
 * env
 * exec
 * export
 * pwd
 * set
 * unset
 * man
 * uname
 * history

Previous Chapter | Next Chapter