The Linux Command Line Gettingcomfortablewith GNU and UNIX Commands
The Linux Command Line Gettingcomfortablewith GNU and UNIX Commands
GUIs are fine, but to unlock the real power of Linux, there's no substitute for the command
line. In this tutorial, Ian Shields introduces you to some of the major features of the bash
shell, with an emphasis on the features that are important for LPI certification. By the end of
this tutorial, you will be comfortable using basic Linux commands like echo and exit, setting
environment variables, and gathering system information. Use the material in this tutorial to
study for the Linux Professional Institute LPIC-1: Linux Server Professional Certification exam
101, or just to learn for fun.
View more content in this series
Overview
This tutorial gives you a brief introduction to some of the major features of the bash shell, and
covers the following topics:
sh, but it provides many improvements in both function and programming capability. It incorporates
features from the Korn shell (ksh) and C shell (csh), and is intended to be a POSIX-compliant
shell.
Unless otherwise noted, the examples in this tutorial use Fedora 22, with a 4.0.4 kernel. Your
results on other systems may differ.
See "Learn Linux, 101: A roadmap for LPIC-1" for a description of and link to each tutorial in
this series. The roadmap is in progress and reflects the version 4.0 objectives of the LPIC-1
exams as updated April 15th, 2015. As tutorials are completed, they will be added to the
roadmap.
This tutorial helps you prepare for Objective 103.1 in Topic 103 of the Linux Server Professional
(LPIC-1) exam 101. The objective has a weight of 4.
Prerequisites
To get the most from the tutorials in this series, you should have a basic knowledge of Linux
and a working Linux system on which you can practice the commands covered in this tutorial.
Sometimes different versions of a program will format output differently, so your results may not
always look exactly like the listings and figures shown here.
Before we delve deeper into bash, recall that a shell is a program that accepts and executes
commands. It also supports programming constructs, allowing complex commands to be built
from smaller parts. These complex commands, or scripts, can be saved as files to become new
commands in their own right. Indeed, many commands on a typical Linux system are scripts.
Shells have some builtin commands, such as cd, break, and exec. Other commands are external.
Input streams provide input to programs, usually from terminal keystrokes. Output streams print
text characters, usually to the terminal. The terminal was originally an ASCII typewriter or display
terminal, but it is now more often a window on a graphical desktop. More detail on how to redirect
these standard I/O streams will be covered in another tutorial in this series.
For the rest of this tutorial, we will assume you know how to get a shell prompt. If you don't, the
developerWorks tutorial "Basic tasks for new Linux users" shows you how to do this and more.
If you are using a Linux system without a graphical desktop, or if you open a terminal window on a
graphical desktop, you will be greeted by a prompt, perhaps like one of the three shown in Listing
1.
Notice that these three prompts are all from my test system atticf20, but for different users. The
first two are bash prompts and both show the logged in user, system name and current working
directory. The third is the default prompt on my system for a ksh shell. Different distributions and
different shells default to different prompts, so dont panic if your distribution looks different. We'll
cover how to change your prompt string in another tutorial in this series.
If you log in as the root user (or superuser), your prompt may look like one of those shown in
Listing 2.
The root user has considerable power, so use it with caution. When you have root privileges,
most prompts include a trailing pound sign (#). Ordinary user privileges are usually delineated by
a different character, commonly a dollar sign ($). Your actual prompt may look different than the
examples in this tutorial. Your prompt may include your user name, hostname, current directory,
date, or time that the prompt was printed, and so on.
Note: Some systems, such as Debian and Debian-based distributions such as Ubuntu, do
not allow root login and require all privileged (root) commands to be executed using the sudo
command. In this case, your prompt will not change, but you will know that you have to use sudo to
execute commands that an ordinary user does not have the power to execute.
These tutorials include code examples that are cut and pasted from real Linux systems using the
default prompts for those systems. Our root prompts have a trailing #, so you can distinguish them
from ordinary user prompts, which have a trailing $. This convention is consistent with many books
on the subject. If something doesn't appear to work for you, check the prompt in the example.
If a line contains a # character, then all remaining characters on the line are ignored. So a #
character may indicate a comment as well as a root prompt. Which it is should be evident from the
context.
Echo
The echo command prints (or echos) its arguments to the terminal as shown in Listing 3.
In the third example of Listing 3, all the extra spaces were compressed down to single spaces in
the output. To avoid this, you need to quote strings, using either double quotes (") or single quotes
('). Bash uses white space, such as blanks, tabs, and new line characters, to separate your input
line into tokens, which are then passed to your command. Quoting strings preserves additional
white space and makes the whole string a single token. In the example above, each token after the
command name is a parameter, so we have respectively 1, 2, 4, and 1 parameters.
The echo command has a couple of options. Normally, echo will append a trailing new line
character to the output. Use the -n option to suppress this. Use the -e option to enable certain
backslash escaped characters to have special meaning. Some of these are shown in Table 1.
\a Alert (bell)
\b Backspace
\n New line
\r Carriage return
\t Horizontal tab
For the sequences described above to be properly handled by the echo command or one of the
many other commands that use similarly escaped control characters, you must include the escape
sequences in quotes, or as part of a quoted string, unless you use a second backslash to have the
shell preserve one for the command. Listing 4 shows some examples of the various uses of \.
Note that bash displays a special prompt (>) when you type a line with unmatched quotes. Your
input string continues onto a second line and includes the new line character.
|
&
;
(
)
<
>
We will discuss some of these in more detail in other parts of this tutorial. For now, note that if you
want to include a metacharacter as part of your text, it must be either quoted or escaped using a
backslash (\) as shown in Listing 4.
The new line and certain metacharacters or pairs of metacharacters also serve as control
operators. These are:
||
&&
&
;
;;
|
(
)
Some of these control operators allow you to create sequences or lists of commands.
The simplest command sequence is just two commands separated by a semicolon (;). Each
command is executed in sequence. In any programmable environment, commands return an
indication of success or failure; Linux commands usually return a zero value for success and a
non-zero value in the event of failure. You can introduce some conditional processing into your list
using the && and || control operators. If you separate two commands with the control operator &&,
then the second is executed if, and only if, the first returns an exit value of zero. If you separate
the commands with ||, then the second one is executed only if the first one returns a non-zero exit
code. Listing 5 shows some command sequences using the echo command. These aren't very
exciting since echo returns 0, but you will see more examples later when we have a few more
commands to use.
Exit
You can terminate a shell using the exit command. You may optionally give an exit code as a
parameter. If you are running your shell in a terminal window on a graphical desktop, your window
will close. Similarly, if you have connected to a remote system using ssh or telnet (for example),
your connection will end. In the bash shell, you can also hold the Ctrl key and press the d key to
exit.
Let's look at another control operator. If you enclose a command or a command list in parentheses,
then the command or sequence is executed in a sub shell, so the exit command exits the sub shell
rather than exiting the shell you are working in. Listing 6 shows a simple example in conjunction
with && and || and two different exit codes.
Environment variables
When you are running in a bash shell, many things constitute your environment, such as the form
of your prompt, your home directory, your working directory, the name of your shell, files that
you have opened, functions that you have defined, and so on. Your environment includes many
variables that may have been set by bash or by you. The bash shell also allows you to have shell
variables, which you may export to your environment for use by other processes running in the
shell or by other shells that you may spawn from the current shell.
Both environment variables and shell variables have a name. You reference the value of a variable
by prefixing its name with '$'. Some of the common bash environment variables that you will
encounter are shown in Table 2.
$ The process id (or PIDof the running bash shell (or other) process
PPID The process id of the process that started this process (that is, the id of
the parent process)
Listing 7 shows what you might see in some of these common bash variables.
Use the
chsh -s /bin/bash
command to change your default shell. The default will take effect next time you log in.
Use the
su - $USER -s /bin/bash
command to create another process as a child of your current shell. The new process
will be a login shell using bash.
Create an id with a default of a bash shell to use for LPI exam preparation.
You may create or set a shell variable by typing a name followed immediately by an equal sign (=).
If the variable exists, you will modify it to assign the new value. Variables are case sensitive, so
var1 and VAR1 are different variables. By convention, variables, particularly exported variables,
are upper case, but this is not a requirement. Technically, $$ and $? are shell parameters rather
than variables. They may only be referenced; you cannot assign a value to them.
When you create a shell variable, you will often want to export it to the environment so it will
be available to other processes that you start from this shell. Variables that you export are not
available to a parent shell. You use the export command to export a variable name. As a shortcut
in bash, you can assign a value and export a variable in one step.
To illustrate assignment and exporting, let's run the bash command while in the bash shell and
then run the Korn shell (ksh) from the new bash shell. We will use the ps command to display
information about the command that is running. We'll learn more about ps in another tutorial in this
series.
/bin/bash
Notes:
1. At the start of this sequence, the bash shell had PID 3175.
2. The second bash shell has PID 4325, and its parent is PID 3175, the original bash shell.
3. We created VAR1, VAR2, and VAR3 in the second bash shell, but only exported VAR2 and
VAR3.
4. In the Korn shell, we created VAR4. The echo command displayed values only for VAR2,
VAR3, and VAR4, confirming that VAR1 was not exported. Were you surprised to see that
the value of the SHELL variable had not changed, even though the prompt had changed?
You cannot always rely on SHELL to tell you what shell you are running under, but the ps
command does tell you the actual command. Note that ps puts a hyphen (-) in front of the first
bash shell to indicate that this is the login shell.
5. Back in the second bash shell, we can see VAR1, VAR2, and VAR3.
6. And finally, when we return to the original shell, none of our new variables still exist.
The earlier discussion of quoting mentioned that you could use either single or double quotes.
There is an important difference between them. The shell expands shell variables that are between
double quotes ($quot;), but expansion is not done when single quotes (') are used. In the previous
example, we started another shell within our shell and we got a new process id. Using the -c
option, you can pass a command to the other shell, which will execute the command and return. If
you pass a quoted string as a command, your outer shell will strip the quotes and pass the string.
If double quotes are used, variable expansion occurs before the string is passed, so the results
may not be as you expect. The shell and command will run in another process so they will have
another PID. Listing 9 illustrates these concepts. The PID of the top-level bash shell is highlighted.
So far, all our variable references have terminated with white space, so it has been clear where
the variable name ends. In fact, variable names may be composed only of letters, numbers or
the underscore character. The shell knows that a variable name ends where another character is
found. Sometimes you need to use variables in expressions where the meaning is ambiguous. In
such cases, you can use curly braces to delineate a variable name as shown in Listing 10.
Env
The env command without any options or parameters displays the current environment variables.
You can also use it to execute a command in a custom environment. The -i (or just -) option
clears the current environment before running the command, while the -u option unsets
environment variables that you do not wish to pass.
Listing 11 shows partial output of the env command without any parameters and then three
examples of invoking different shells without the parent environment. Look carefully at these before
we discuss them.
Note: If your system does not have the ksh (Korn) or tcsh shells installed, you will need to install
them to do these exercises yourself.
VTE_VERSION=4002
HISTSIZE=1000
GJS_DEBUG_OUTPUT=stderr
WINDOWID=35651982
GJS_DEBUG_TOPICS=JS ERROR;JS LOG
QT_GRAPHICSSYSTEM_CHECKED=1
USER=ian
..._=/usr/bin/env
OLDPWD=/home/ian/Documents
[ian@atticf20 ~]$ env -i bash -c 'echo $SHELL; env'
/bin/bash
PWD=/home/ian
SHLVL=1
_=/usr/bin/env
[ian@atticf20 ~]$ env -i ksh -c 'echo $SHELL; env'
/bin/sh
_=*3175*/usr/bin/env
PWD=/home/ian
SHLVL=1
_AST_FEATURES=UNIVERSE - ucb
A__z="*SHLVL
[ian@atticf20 ~]$ env -i tcsh -c 'echo $SHELL; env'
SHELL: Undefined variable.
When bash sets the SHELL variable, it exports it to the environment, There are three other
variables that the new bash shell has created in the environment. In the ksh example, we have
five environment variables, but our attempt to echo the value of the SHELL variable gives /bin/sh.
Some earlier versions of ksh simply gave a blank line indicating that the SHELL variable was not
set. Finally, tcsh has not created any environment variables and produces an error at our attempt
to reference the value of SHELL.
So let's consider our attempt to display the SHELL variable's value in these non-login shells:
1. When bash started, it set the SHELL variable, but it did not automatically export it to the
environment.
2. When ksh started, it set its view of the SHELL variable to /bin/sh. Compare this with the
earlier example where ksh inherited the exported value of /bin/bash from the calling bash
shell.
3. When tcsh started, it did not set the SHELL variable. In this case, the default behavior is
different than ksh (and bash) in that an error is reported when we attempt to use a variable
that does not exist.
You can use the unset command to unset a variable and remove it from the shell variable list. If
the variable was exported to the environment, this will also remove it from the environment. You
can use the set command to control many facets of the way bash (or other shells) work. Set is a
shell builtin, so the various options are shell specific. In bash, the -u option causes bash to report
an error with undefined variables rather than treat them as defined but empty. You can turn on the
various options to set with a - and turn them off with a +. You can display currently set options
using echo $-.
himBH
If you use the set command without any options, it displays all your shell variables and their
values (if any). There is also another command, declare, which you can use to create, export, and
display values of shell variables. You can explore the many remaining set options and the declare
command using the man pages. We will discuss man pages later in this tutorial.
Exec
One final command to cover is exec. You can use the exec command to run another program that
replaces the current shell. Listing 13 starts a child bash shell and then uses exec to replace it with
a Korn shell. Upon exit from the Korn shell, you are back at the original bash shell (PID 2852, in
this example).
Listing 14 is from a Fedora 22 system running on a 64-bit AMD CPU. The uname command is
available on most UNIX and UNIX-like systems, as well as Linux. The information printed will
vary by Linux distribution and version as well as by the type of machine you are running on. Listing
15 shows the output from an older Intel 32-bit system running Ubuntu 14.04 in live mode from a
DVD.
Command history
If you are typing in commands as you read, you may notice that you often use a command many
times, either exactly the same or with slight changes. The good news is that the bash shell
can maintain a history of your commands. By default, history is on. You can turn it off using the
command set +o history and turn it back on using set -o history. An environment variable
called HISTSIZE tells bash how many history lines to keep. A number of other settings control how
history works and is managed. See the bash man pages for full details.
Some of the commands that you can use with the history facility are:
history
Displays the entire history.
history N
Displays the last N lines of your history.
history -d N
Deletes line N form your history; you might do this if the line contains a password, for
example.
!!
Your most recent command.
!N
The Nth history command.
!-N
The command that is N commands back in the history (!-1 is equivalent to !!).
!#
The current command you are typing.
!string
The most recent command that starts with string.
!?string?
The most recent command that contains string.
You can also use a colon (:) followed by certain values to access or modify part or a command
from your history. Listing 16 illustrates some of the history capabilities.
C-d Del Delete the character under the cursor (Del and
Backspace functions may be configured with
opposite meanings).
If you prefer to manipulate the history using a vi-like editing mode, use the command set -o
vi to switch to vi mode. Switch back to emacs mode using set -o emacs. When you retrieve a
command in vi mode, you are initially in vi's insert mode. The vi editor is covered in another tutorial
in this series. (See Related topics for the series roadmap).
When you close a shell session or logoff bash saves the last $HISTSIZE history lines in your
home directory in a file called .bash_history (~/.bash_history). When you login again your history
is loaded from this file. If you have multiple sessions open, then the history file is rewritten as each
one closes. There is no merging of data from each session.
If you want to know what command will be executed if you type a particular string, use the which or
type command. Listing 17 shows my default path along with the locations of several commands.
Note that the directories in the path mostly end in /bin. This is a common convention, but not
a requirement. The which command reported that the ls command is an alias and that the set
command could not be found. In this case, we interpret that to mean that it does not exist or that
it is a builtin. The type command reports that the ls command is an alias, but it identifies the set
command as a shell builtin. It also reports that there is a builtin echo command as well as the one
in /bin that was found by which. The commands also produce output in different orders.
We saw that the ls command, used for listing directory contents, is an alias. Aliases are a handy
way to configure some commands to use different sets of defaults or to provide an alternate name
for a command. In our example, the --color=tty option causes directory listings to be color coded
according to the type of file or directory. Try running dircolors --print-database to see how the
color coding is controlled and which colors are used for what kind of file.
Each of these commands has additional options. Depending on your need, you may use either
command. I tend to use which when I am reasonably sure I'll find an executable and I just need its
full path specification. I find that type gives me more precise information, which I sometimes need
in a shell script.
Absolute paths are those starting with /, such as those we saw in Listing 17 (/bin/bash, /bin/
env, etc).
Relative paths are relative to your current working directory, as reported by the pwd command.
These commands do not start with /, but do contain at least one /.
You may use absolute paths regardless of your current working directory, but you will probably
use relative paths only when a command is close to your current directory. Suppose you are
developing a new version of the classic "Hello World!" program in a subdirectory of your home
directory called mytestbin. You might use the relative path to run your command as mytestbin/
hello. There are two special names you can use in a path; a single dot (.) refers to the current
directory, and a pair of dots (..) refers to the parent of the current directory. Because your home
directory is usually not on your PATH (and generally should not be), you will need to explicitly
provide a path for any executable that you want to run from your home directory. For example, if
you had a copy of your hello program in your home directory, you could run it using the command
./hello. You can use both . and .. as part of an absolute path, although a single . is not very useful
in such a case. You can also use a tilde (~) to mean your own home directory and ~username to
mean the home directory of the user named username. Some examples are shown in Listing 18.
As for commands, there is also an environment variable, CDPATH, which contains a colon-
separated set of directories that should be searched (in addition to the current working directory)
when resolving relative paths. If resolution used a path from CDPATH, then cd will print the full
path of the resulting directory as output. Normally, a successful directory change results in no
output other than a new, and possibly changed, prompt. Some examples are shown in Listing 19.
A heading with the name of the command followed by its section number in parentheses.
The name of the command and any related commands that are described on the same man
page.
A synopsis of the options and parameters applicable to the command.
A short description of the command.
Detailed information on each of the options.
You might find other sections on usage, how to report bugs, author information, and a list of
related commands. Related commands and their section are usually found under SEE ALSO. For
example, the man page for man lists the related commands shown in Listing 20.
There are eight common manual page sections. Manual pages are usually installed when you
install a package, so if you do not have a package installed, you probably won't have a manual
page for it. Similarly, some of your manual sections may be empty or nearly empty. The common
manual sections, with some example contents are:
Other sections that you might find include 9 for Linux kernel documentation, n for new
documentation, o for old documentation, and l for local documentation.
Some entries appear in multiple sections. Our examples in Listing 21 below show mkdir in sections
1 and 2, and tty in sections 1 and 4. You can specify a particular section, for example, man 4 tty or
man 2 mkdir, or you can specify the -a option to list all applicable manual sections.
The man command has many options and related command for you to explore on your own. For
now, let's take a quick look at some of the "See also" commands related to man.
See also
Two important commands related to man are whatis and apropos. The whatis command searches
man pages for the name you give and displays the name information from the appropriate manual
pages. The apropos command does a keyword search of manual pages and lists ones containing
your keyword.
The man command pages output onto your display using a paging program. On most Linux
systems, this is likely to be the less program. Another choice might be the older more program. If
you wish to print the page, specify the -t option to format the page for printing using the groff or
troff program.
The less pager has several commands that help you search for strings within the displayed output.
Use man less to find out more about / (search forwards), ? (search backwards), and n (repeat last
search), among many other commands.
Bash help
The man page for bash is very long and it can take some time to search through it, even using the
less pager. Fortunately, if you just want some quick help on a bash builtin you can use the help
builtin as shown in Listing 22. Use help with no arguments to get a list of available help and use
help help to find out how to use help.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
Options:
-L force symbolic links to be followed: resolve symbolic links in
DIR after processing instances of `..'
Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.
There are also some graphical interfaces to man pages, such as xman (from the XFree86 Project)
and yelp (the Gnome 2.0 help browser).
If you can't find help for a command, try running the command with the --help option. This may
provide the command's help, or it may tell you how to get the help you need.
Related topics
developerWorks Premium provides an all-access pass to powerful tools, curated technical
library from Safari Books Online, conference discounts and proceedings, SoftLayer and
Bluemix credits, and more.
Use the developerWorks roadmap for LPIC-1 to find the developerWorks tutorials to help you
study for LPIC-1 certification based on the LPI Version 4.0 April 2015 objectives.
At the Linux Professional Institute website, find detailed objectives, task lists, and sample
questions for the certifications. In particular, see:
The LPIC-1: Linux Server Professional Certification program details
LPIC-1 exam 101 objectives
LPIC-1 exam 102 objectives
Always refer to the Linux Professional Institute website for the latest objectives.
The Linux Documentation Project has a variety of useful documents, especially its HOWTOs.