# 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.
Most Common Shells :
| The Bourne shell | /bin/sh |
| The Bourne again shell | /bin/bash |
| The Korn shell | /bin/ksh |
| The C shell | /bin/csh |
| Tom's C shell | /bin/tcsh |
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.
* $ echo “this is a short line”
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
* $ /usr/X11R6/bin/xeyes
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
* $ ./xeyes
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)
* $ xeyes

## 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.
| 1. initialise a variable: | Variable-Name=value (no spaces !!) |
| 2. reference a variable: | $Variable-Name |
* CREDIT=300
* echo $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
The set and env commands :
| set | Lists all variables |
| env | Lists all exported 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.
* export CREDIT
* env | grep CREDIT
List of common predefined variables
| PREDEFINED VARIABLES | MEANING |
| DISPLAY | Used by X to identify where to run a client application |
| HISTFILE | Path to the user's .bash_history file |
| HOME | The path to the user's home |
| LOGNAME | The name used by the user to log in |
| PATH | List of directories searched by the shell for programs to be executed when a command is entered without a path. |
| PWD | The current working directory |
| SHELL | The shell used (bash in most Linux distributions) |
| TERM | The current terminal emulation |
Special variables
The next few variables are related to process management.
* $!represents the PID value of the last child process
* $$represents the PID of the running shell
* $?is 0 if the last command was executed successfully and non-zero otherwise
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.
* $ ls /usr/bin/b*
lists all programs starting with a 'b'
* The ? wildcard replaces any one character.
* $ ls /usr/bin/?b*
lists all programs having a 'b' as the second letter
* [ ] is used to define a range of values.
* $ ls a[0-9]
* $ ls [!Aa]*
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.
* $ mkdir {mon, tues, wednes} day
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.
* $ TIME="Today's date is `date +%a:%d:%b`”
* echo $TIME
* Today's date is Sun:15:Jul
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.
* $ TIME=$(date)

## The Command History

To view the list of previously typed commands you can use the bash built-in command history.
* $ history
* ls
* grep 500 /etc/passwd
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.
Emacs Key Bindings for Editing the Command History :
| Ctrl+P | Previous line (same as Up-arrow) |
| Ctrl+n | Next line (same as Down-arrow) |
| Ctrl+b | Go back one character on the line (same as Left-Arrow) |
| Ctrl+f | Go forward one character on the line (Same as Right-Arrow) |
| Ctrl+a | Go to the beginning of the line (Same as <Home>) |
| Ctrl+e | Go to the end of the line (Same as <End>) |
The bang (!) key can be used to rerun a command.
Example:
| x executes the latest command in the history list starting with an 'x' | 2 runs command number 2 from the history output | -2 runs the command before last | ! runs the last command ^string1^string2 run previous command and replace string1 by string2 |
Other Commands
Aliases
You can create aliases for commands needing many arguments. The format to create an alias is
* $ alias myprog='command [options]{arguments}'
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.
| command1; command2; command3 | The three commands are run in sequence regardless of the success of the previous command |
| command1 && command2 && command3 | Each command will execute only if the previous exit code is 0 (success) |
| command1 || command2 || command3 | The next command will execute only if the previous exit code is not 0 (failure) |
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.
* $ echo $$
* 414
* $ bash
* $ echo $$
* 455
* $ echo hello
* hello
* $ echo $$
* 455
* $ exec echo hello
* hello
* $ echo $$
* 414
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

The manpages are organised in specific topics :
| NAME | the name of the item followed by a short one line description. |
| SYNOPSYS | the syntax for the command |
| DESCRIPTION | a longer description |
| OPTIONS | a review of all possible options and their function |
| FILES | files that are related to the current item (configuration files etc) |
| SEE ALSO | other manpages related to the current topic |
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:
* name(key) – one line description
The syntax for whatis is:
whatis <string>
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 <string>
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)
* whatis lilo
* lilo (8) - install boot loader
* lilo.conf [lilo] (5) - configuration file for lilo
* man -k lilo
* grubby (8) - command line tool for configuring grub, lilo, and elilo
* lilo (8) - install boot loader
* lilo.conf [lilo] (5) - configuration file for lilo
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.
Manpage Sections :
| Section 1 | Information on executables |
| Section 2 | System calls, e.g mkdir(2) |
| Section 3 | Library calls, e.g stdio(3) |
| Section 4 | Devices (files in /dev) |
| Section 5 | Configuration files and formats |
| Section 6 | Games |
| Section 7 | Macro packages |
| Section 8 | Administration commands |
| Section 9 | Kernel routines |
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:
* [[Image:]]
* $ man mkdir
* $ man 2 mkdir
* $ man crontab
* $ man 5 crontab
file
file is used to try and detect what type a particular file is.
For example
* $ file picture.png
* picture.png: PNG image, 179 x 179, 8-bit/color RGBA, non-interlaced
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.
* $ uname -r
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