Learning The Shell
Learning The Shell
On most Linux systems a program called bash (which stands for Bourne Again SHell,
an enhanced version of the original Unix shell program, sh, written by Steve Bourne)
acts as the shell program. Besides bash, there are other shell programs available for
Linux systems. These include: ksh, tcsh and zsh.
What's a "Terminal?"
It's a program called a terminal emulator. This is a program that opens a window and
lets you interact with the shell. There are a bunch of different terminal emulators we
can use. Some Linux distributions install several. These might include gnome-
terminal, konsole, xterm, rxvt, kvt, nxterm, and eterm.
Starting a Terminal
Window managers usually have a way to launch a terminal from the menu. Look
through the list of programs to see if anything looks like a terminal emulator. While
there are a number of different terminal emulators, they all do the same thing. They
give us access to a shell session. You will probably develop a preference for one,
based on the different bells and whistles it provides.
[me@linuxbox me]$
Excellent! Now type some nonsense characters and press the enter key.
If all went well, we should have gotten an error message complaining that it cannot
understand the command:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ kdkjflajfks
bash: kdkjflajfks: command not found
Wonderful! Now press the up-arrow key. Watch how our previous command
"kdkjflajfks" returns. Yes, we have command history. Press the down-arrow and we
get the blank line again.
Recall the "kdkjflajfks" command using the up-arrow key if needed. Now, try the left
and right-arrow keys. We can position the text cursor anywhere in the command line.
This allows us to easily correct mistakes.
Besides using the mouse to scroll the contents of the terminal window, we can can use
it to copy text. Drag the mouse over some text (for example, "kdkjflajfks" right here
on the browser window) while holding down the left button. The text should highlight.
Release the left button and move the mouse pointer to the terminal window and press
the middle mouse button (alternately, press both the left and right buttons at the
same time when working on a touch pad). The text we highlighted in the browser
window should be copied into the command line.
Further Reading
The Wikipedia entry for Steve Bourne, developer of the original Bourne shell
The Wikipedia article on the Unix shell, the place where all this fun got started
The "Power Terminals" Adventure
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next
Navigation
In this lesson, we will introduce our first three commands: pwd (print working
directory), cd (change directory), and ls (list files and directories).
Those new to the command line will need to pay close attention to this lesson since
the concepts will take some getting used to.
Most graphical environments include a file manager program used to view and
manipulate the contents of the file system. Often we will see the file system
One important difference between Windows and Unix-like operating systems such as
Linux is that Linux does not employ the concept of drive letters. While Windows drive
letters split the file system into a series of different trees (one for each device), Linux
always has a single tree. Different storage devices may be different branches of the
tree, but there is always just a single tree.
pwd
Since the command line interface cannot provide graphic pictures of the file system
structure, we must have a different way of representing it. To do this, think of the file
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
system tree as a maze, and that we are standing in it. At any given moment, we are
located in a single directory. Inside that directory, we can see its files and the pathway
to its parent directory and the pathways to the subdirectories of the directory in which
we are standing.
The directory we are standing in is called the working directory. To see the name of
the working directory, we use the pwd command.
When we first log on to our Linux system, the working directory is set to our home
directory. This is where we put our files. On most systems, the home directory will be
called /home/user_name, but it can be anything according to the whims of the system
administrator.
[me@linuxbox me]$ ls
Desktop Downloads foo.txt Pictures Templates
Documents examples.desktop Music Public Videos
We will come back to ls in the next lesson. There are a lot of fun things you can do
with it, but we have to talk about pathnames and directories a bit first.
cd
To change the working directory (where we are standing in the maze) we use the cd
command. To do this, we type cd followed by the pathname of the desired working
directory. A pathname is the route we take along the branches of the tree to get to the
directory we want. Pathnames can be specified two different ways; absolute
pathnames or relative pathnames. Let's look with absolute pathnames first.
An absolute pathname begins with the root directory and follows the tree branch by
branch until the path to the desired directory or file is completed. For example, there
is a directory on your system in which most programs are installed. The pathname of
the directory is /usr/bin. This means from the root directory (represented by the
leading slash in the pathname) there is a directory called "usr" which contains a
directory called "bin".
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
aa-enabled mtrace
aa-exec mtr-packet
aclocal mtvtoppm
aclocal-1.15 mtype
aconnect mutter
acpi_listen mxtar
add-apt-repository mzip
addpart namei
Now we can see that we have changed the current working directory to /usr/bin and
that it is full of files. Notice how the shell prompt has changed? As a convenience, it is
usually set up to display the name of the working directory.
Where an absolute pathname starts from the root directory and leads to its
destination, a relative pathname starts from the working directory. To do this, it uses a
couple of special notations to represent relative positions in the file system tree. These
special notations are "." (dot) and ".." (dot dot).
The "." notation refers to the working directory itself and the ".." notation refers to the
working directory's parent directory. Here is how it works. Let's change the working
directory to /usr/bin again:
O.K., now let's say that we wanted to change the working directory to the parent of
/usr/bin which is /usr. We could do that two different ways. First, with an absolute
pathname:
me@linuxbox bin]$ cd ..
me@linuxbox usr]$ pwd
/usr
Two different methods with identical results. Which one should we use? The one that
requires the least typing!
Likewise, we can change the working directory from /usr to /usr/bin in two different
ways. First using an absolute pathname:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Or, with a relative pathname:
Now, there is something important that we must point out here. In most cases, we
can omit the "./". It is implied. Typing:
A Few Shortcuts
If we type cd followed by nothing, cd will change the working directory to our home
directory.
A related shortcut is to type cd ~user_name. In this case, cd will change the working
directory to the home directory of the specified user.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next
Looking Around
Now that we know how to move from working directory to working directory, we're
going to take a tour of our Linux system and, along the way, learn some things about
what makes it tick. But before we begin, we have to learn about some tools that will
come in handy during our journey. These are:
ls
The ls command is used to list the contents of a directory. It is probably the most
commonly used Linux command. It can be used in a number of different ways. Here
are some examples:
Command Result
ls /bin List the files in the /bin directory (or any other directory we
care to specify)
ls -l /etc /bin
List the files in the /bin directory and the /etc directory in
long format
ls -la .. List all files (even ones with names beginning with a period
character, which are normally hidden) in the parent of the
working directory in long format
Examples of the ls command
These examples also point out an important concept about commands. Most
commands operate like this:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
where command is the name of the command, -options is one or more adjustments to
the command's behavior, and arguments is one or more "things" upon which the
command operates.
In the case of ls, we see that ls is the name of the command, and that it can have
one or more options, such as -a and -l, and it can operate on one or more files or
directories.
If we use the -l option with ls, you will get a file listing that contains a wealth of
information about the files being listed. Here's an example:
File Name
The name of the file or directory.
Modification Time
The last time the file was modified. If the last modification occurred more than
six months in the past, the date and year are displayed. Otherwise, the time of
day is shown.
Size
The size of the file in bytes.
Group
The name of the group that has file permissions in addition to the file's owner.
Owner
The name of the user who owns the file.
File Permissions
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
A representation of the file's access permissions. The first character is the type of
file. A "-" indicates a regular (ordinary) file. A "d" indicates a directory. The
second set of three characters represent the read, write, and execution rights of
the file's owner. The next three represent the rights of the file's group, and the
final three represent the rights granted to everybody else. We'll discuss this in
more detail in a later lesson.
less
less is a program that lets us view text files. This is very handy since many of the
files used to control and configure Linux are human readable.
less text_file
Controlling less
Once started, less will display the text file one page at a time. We can use the Page
Up and Page Down keys to move through the text file. To exit less, we type "q". Here
are some commands that less will accept:
Command Action
/characters Search forward in the text file for an occurrence of the specified
characters
q Quit
Keyboard commands for the less program
file
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
As we wander around our Linux system, it is helpful to determine what kind of data a
file contains before we try to view it. This is where the file command comes in. file
will examine a file and tell us what kind of file it is.
file name_of_file
The file program can recognize most types of files, such as:
GNU tar archive A tape archive file. A common way of no, use tar tvf to
storing groups of files. view listing.
While it may seem that most files cannot be viewed as text, a surprising number can
be. This is especially true of the important configuration files. During our adventure
we will see that many features of the operating system are controlled by text
configuration files and shell scripts. In Linux, there are no secrets!
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next
A Guided Tour
It's time to take our tour. The table below lists some interesting places to explore. This
is by no means a complete list, but it should prove to be an interesting adventure. For
each of the directories listed below, do the following:
Directory Description
/ The root directory where the file system begins. The root
directory will probably contain only subdirectories.
/boot This is where the Linux kernel and boot loader files are kept.
The kernel is a file called vmlinuz.
/etc The /etc directory contains the configuration files for the
system. All of the files in /etc should be text files. Some
points of interest are:
/etc/passwd
The passwd file contains the essential information for
each user. This is where user accounts are defined.
/etc/fstab
The fstab file contains a table of devices that get
mounted when the system boots. This file defines the
system's disk drives.
/etc/hosts
This file lists the network host names and IP addresses
that are intrinsically known to the system.
/etc/init.d
This directory contains the scripts that start various
system services at boot time.
/bin, /usr/bin These two directories contain most of the programs for the
system. The /bin directory has the essential programs that
the system requires to operate, while /usr/bin contains
applications for the system's users.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
/sbin, /usr/sbin The sbin directories contain programs for system
administration, mostly for use by the superuser.
/usr/share/X11
Support files for the X Window system
/usr/share/dict
Dictionaries for the spelling checker. Yes, Linux comes
with a spelling checker. See look and aspell.
/usr/share/doc
Various documentation files in a variety of formats.
/usr/share/man
The man pages are kept here.
/usr/local /usr/local and its subdirectories are used for the installation
of software and other files for use on the local machine. What
this really means is that software that is not part of the
official distribution (which usually goes in /usr/bin) goes
here.
/var The /var directory contains files that change as the system is
running. This includes:
/var/log
Directory that contains log files. These are updated as
the system runs. It's a good idea to view the files in this
directory from time to time, to monitor the health of
your system.
/var/spool
This directory is used to hold files that are queued for
some process, such as mail messages and print jobs.
When a user's mail first arrives on the local system
(assuming it has local mail, a rare occurrence on modern
machines that are not mail servers), the messages are
first stored in /var/spool/mail
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
/tmp /tmp is a directory in which programs can write their
temporary files.
/proc The /proc directory is also special. This directory does not
contain files. In fact, this directory does not really exist at all.
It is entirely virtual. The /proc directory contains little peep
holes into the kernel itself. There are a group of numbered
entries in this directory that correspond to all the processes
running on the system. In addition, there are a number of
named entries that permit access to the current configuration
of the system. Many of these entries can be viewed. Try
viewing /proc/cpuinfo. This entry will tell you what the
kernel thinks of the system's CPU.
Further Reading
To learn more about the organization of the Linux filesystem, consult the
Filesystem Hierarchy Standard
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next
Manipulating Files
This lesson will introduce the following commands:
These four commands are among the most frequently used Linux commands. They
are the basic commands for manipulating both files and directories.
Now, to be frank, some of the tasks performed by these commands are more easily
done with a graphical file manager. With a file manager, you can drag and drop a file
from one directory to another, cut and paste files, delete files, etc. So why use these
old command line programs?
The answer is power and flexibility. While it is easy to perform simple file
manipulations with a graphical file manager, complicated tasks can be easier with the
command line programs. For example, how would you copy all the HTML files from
one directory to another, but only copy files that did not exist in the destination
directory or were newer than the versions in the destination directory? Pretty hard
with with a file manager. Pretty easy with the command line:
Wildcards
Before we begin with our commands, we'll first look at a shell feature that makes
these commands so powerful. Since the shell uses filenames so much, it provides
special characters to help you rapidly specify groups of filenames. These special
characters are called wildcards. Wildcards allow you to select filenames based on
patterns of characters. The table below lists the wildcards and what they select:
Wildcard Meaning
[characters] Matches any character that is a member of the set characters. The
set of characters may also be expressed as a POSIX character
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
class such as one of the following:
[:alnum:] Alphanumeric characters
[:digit:] Numerals
[!characters] Matches any character that is not a member of the set characters
Summary of wildcards and their meanings
Pattern Matches
*
All filenames
g*
All filenames that begin with the character "g"
b*.txt
All filenames that begin with the character "b" and end with the
characters ".txt"
Data???
Any filename that begins with the characters "Data" followed by
exactly 3 more characters
[abc]*
Any filename that begins with "a" or "b" or "c" followed by any
other characters
[[:upper:]]*
Any filename that begins with an uppercase letter. This is an
example of a character class.
BACKUP.
[[:digit:]] Another example of character classes. This pattern matches any
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[[:digit:]] filename that begins with the characters "BACKUP." followed by
exactly two numerals.
*[!
[:lower:]] Any filename that does not end with a lowercase letter.
We can use wildcards with any command that accepts filename arguments.
cp
The cp program copies files and directories. In its simplest form, it copies a single file:
It can also be used to copy multiple files (and/or directories) to a different directory:
A note on notation: ... signifies that an item can be repeated one or more times.
Command Results
cp file1 file2 Copies the contents of file1 into file2. If file2 does not exist,
it is created; otherwise, file2 is silently overwritten
with the contents of file1.
cp -i file1 file2 Like above however, since the "-i" (interactive) option is
specified, if file2 exists, the user is prompted before it is
overwritten with the contents of file1.
cp file1 dir1 Copy the contents of file1 (into a file named file1) inside of
directory dir1.
cp -R dir1 dir2 Copy the contents of the directory dir1. If directory dir2 does
not exist, it is created. Otherwise, it creates a directory
named dir1 within directory dir2.
Examples of the cp command
mv
The mv command moves or renames files and directories depending on how it is used.
It will either move one or more files to a different directory, or it will rename a file or
directory. To rename a file, it is used like this:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ mv filename1 filename2
Command Results
mv file1 file2 If file2 does not exist, then file1 is renamed file2. If file2
exists, its contents are silently replaced with the
contents of file1.
mv -i file1 file2 Like above however, since the "-i" (interactive) option is
specified, if file2 exists, the user is prompted before it is
overwritten with the contents of file1.
mv file1 file2 dir1 The files file1 and file2 are moved to directory dir1. If dir1
does not exist, mv will exit with an error.
mv dir1 dir2 If dir2 does not exist, then dir1 is renamed dir2. If dir2
exists, the directory dir1 is moved within directory dir2.
Examples of the mv command
rm
The rm command removes (deletes) files and directories.
Using the recursive option (-r), rm can also be used to delete directories:
Command Results
rm -i file1 file2 Like above however, since the "-i" (interactive) option is
specified, the user is prompted before each file is deleted.
rm -r dir1 dir2 Directories dir1 and dir2 are deleted along with all of their
contents.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Examples of the rm command
Before you use rm with wildcards, try this helpful trick: construct your
command using ls instead. By doing this, you can see the effect of your wildcards
before you delete files. After you have tested your command with ls, recall the
command with the up-arrow key and then substitute rm for ls in the command.
mkdir
The mkdir command is used to create directories. To use it, you simply type:
Command Results
cp *.txt text_files Copy all files in the current working directory with
names ending with the characters ".txt" to an existing
directory named text_files.
mv dir1 ../*.bak dir2 Move the subdirectory dir1 and all the files ending in
".bak" in the current working directory's parent
directory to an existing directory named dir2.
Further Reading
Chapter 4 of The Linux Command Line covers this topic in more detail
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next
1. An executable program like all those files we saw in /usr/bin. Within this
category, programs can be compiled binaries such as programs written in C and
C++, or programs written in scripting languages such as the shell, Perl, Python,
Ruby, etc.
2. A command built into the shell itself. bash provides a number of commands
internally called shell builtins. The cd command, for example, is a shell builtin.
3. A shell function. These are miniature shell scripts incorporated into the
environment. We will cover configuring the environment and writing shell
functions in later lessons, but for now, just be aware that they exist.
4. An alias. Commands that we can define ourselves, built from other commands.
This will be covered in a later lesson.
Identifying Commands
It is often useful to know exactly which of the four kinds of commands is being used
and Linux provides a couple of ways to find out.
type
The type command is a shell builtin that displays the kind of command the shell will
execute, given a particular command name. It works like this:
type command
where “command” is the name of the command we want to examine. Here are some
examples:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ type type
type is a shell builtin
[me@linuxbox me]$ type ls
s is aliased to `ls --color=auto'
[me@linuxbox me]$ type cp
cp is /bin/cp
Here we see the results for three different commands. Notice that the one for ls and
how the ls command is actually an alias for the ls command with the “-- color=auto”
option added. Now we know why the output from ls is displayed in color!
which
which only works for executable programs, not builtins nor aliases that are substitutes
for actual executable programs.
help
bash has a built-in help facility available for each of the shell builtins. To use it, type
“help” followed by the name of the shell builtin. Optionally, we can add the -m option
to change the format of the output. For example:
SYNOPSIS
cd [-L|-P] [dir]
DESCRIPTION
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value o
HOME shell variable.
The variable CDPATH defines the search path for the directory contai
DIR. Alternative directory names in CDPATH are separated by a colon
A null directory name is the same as the current directory. If DIR
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
with a slash (/), then CDPATH is not used.
Options:
-L force symbolic links to be followed
-P use the physical directory structure without following symbo
links
Exit Status:
Returns 0 if the directory is changed; non-zero otherwise.
SEE ALSO
bash(1)
IMPLEMENTATION
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
cd [-L|-P] [dir]
This notation says that the command cd may be followed optionally by either a “-L” or
a “-P” and further, optionally followed by the argument “dir”.
--help
Many executable programs support a “--help” option that displays a description of the
command's supported syntax and options. For example:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Some programs don't support the “--help” option, but try it anyway. Often it results in
an error message that will reveal similar usage information.
man
Most executable programs intended for command line use provide a formal piece of
documentation called a manual or man page. A special paging program called man is
used to view them. It is used like this:
man program
where “program” is the name of the command to view. Man pages vary somewhat in
format but generally contain a title, a synopsis of the command's syntax, a description
of the command's purpose, and a listing and description of each of the command's
options. Man pages, however, do not usually include examples, and are intended as a
reference, not a tutorial. Let's try viewing the man page for the ls command:
On most Linux systems, man uses less to display the manual page, so all of the
familiar less commands work while displaying the page.
Many software packages installed on your system have documentation files residing in
the /usr/share/doc directory. Most of these are stored in plain text format and can
be viewed with less. Some of the files are in HTML format and can be viewed with a
web browser. We may encounter some files ending with a “.gz” extension. This
indicates that they have been compressed with the gzip compression program. The
gzip package includes a special version of less called zless that will display the
contents of gzip-compressed text files.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next
I/O Redirection
In this lesson, we will explore a powerful feature used by command line programs
called input/output redirection. As we have seen, many commands such as ls print
their output on the display. This does not have to be the case, however. By using
some special notations we can redirect the output of many commands to files,
devices, and even to the input of other commands.
Standard Output
Most command line programs that display their results do so by sending their results
to a facility called standard output. By default, standard output directs its contents to
the display. To redirect standard output to a file, the ">" character is used like this:
In this example, the ls command is executed and the results are written in a file
named file_list.txt. Since the output of ls was redirected to the file, no results
appear on the display.
Each time the command above is repeated, file_list.txt is overwritten from the
beginning with the output of the command ls. To have the new results appended to
the file instead, we use ">>" like this:
When the results are appended, the new results are added to the end of the file, thus
making the file longer each time the command is repeated. If the file does not exist
when we attempt to append the redirected output, the file will be created.
Standard Input
Many commands can accept input from a facility called standard input. By default,
standard input gets its contents from the keyboard, but like standard output, it can be
redirected. To redirect standard input from a file instead of the keyboard, the "<"
character is used like this:
In the example above, we used the sort command to process the contents of
file_list.txt. The results are output on the display since the standard output was
not redirected. We could redirect standard output to another file like this:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ sort < file_list.txt > sorted_file_list.txt
As we can see, a command can have both its input and output redirected. Be aware
that the order of the redirection does not matter. The only requirement is that the
redirection operators (the "<" and ">") must appear after the other options and
arguments in the command.
Pipelines
The most useful and powerful thing we can do with I/O redirection is to connect
multiple commands together to form what are called pipelines. With pipelines, the
standard output of one command is fed into the standard input of another. Here is a
very useful example:
In this example, the output of the ls command is fed into less. By using this "|
less" trick, we can make any command have scrolling output.
By connecting commands together, we can accomplish amazing feats. Here are some
examples to try:
Filters
One kind of program frequently used in pipelines is called a filter. Filters take standard
input and perform an operation upon it and send the results to standard output. In
this way, they can be combined to process information in powerful ways. Here are
some of the common programs that can act as filters:
sort Sorts standard input then outputs the sorted result on standard output.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
uniq Given a sorted stream of data from standard input, it removes
duplicate lines of data (i.e., it makes sure that every line is unique).
grep Examines each line of data it receives from standard input and outputs
every line that contains a specified pattern of characters.
fmt Reads text from standard input, then outputs formatted text on
standard output.
pr Takes text input from standard input and splits the data into pages with
page breaks, headers and footers in preparation for printing.
head Outputs the first few lines of its input. Useful for getting the header of
a file.
tail Outputs the last few lines of its input. Useful for things like getting the
most recent entries from a log file.
sed Stream editor. Can perform more sophisticated text translations than
tr.
Further Reading
Chapter 6 of The Linux Command Line covers this topic in more detail.
Chapters 19 through 21 of The Linux Command Line provide an in-depth look at
the text processing tools available in Linux.
To learn more about the AWK programming language, consider the AWK
adventure.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next
Expansion
Each time we type a command line and press the enter key, bash performs several
processes upon the text before it carries out our command. We have seen a couple of
cases of how a simple character sequence, for example “*”, can have a lot of meaning
to the shell. The process that makes this happen is called expansion. With expansion,
we type something and it is expanded into something else before the shell acts upon
it. To demonstrate what we mean by this, let's take a look at the echo command. echo
is a shell builtin that performs a very simple task. It prints out its text arguments on
standard output:
That's pretty straightforward. Any argument passed to echo gets displayed. Let's try
another example:
So what just happened? Why didn't echo print “*”? As we recall from our work with
wildcards, the “*” character means match any characters in a filename, but what we
didn't see in our original discussion was how the shell does that. The simple answer is
that the shell expands the “*” into something else (in this instance, the names of the
files in the current working directory) before the echo command is executed. When
the enter key is pressed, the shell automatically expands any qualifying characters on
the command line before the command is carried out, so the echo command never
saw the “*”, only its expanded result. Knowing this, we can see that echo behaved as
expected.
Pathname Expansion
The mechanism by which wildcards work is called pathname expansion. If we try some
of the techniques that we employed in our earlier lessons, we will see that they are
really expansions. Given a home directory that looks like this:
[me@linuxbox me]$ ls
Desktop
ls-output.txt
Documents Music
Pictures
Public
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Templates
Videos
and:
or even:
Tilde Expansion
As we recall from our introduction to the cd command, the tilde character (“~”) has a
special meaning. When used at the beginning of a word, it expands into the name of
the home directory of the named user, or if no user is named, the home directory of
the current user:
Arithmetic Expansion
The shell allows arithmetic to be performed by expansion. This allow us to use the
shell prompt as a calculator:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
$((expression))
Arithmetic expansion only supports integers (whole numbers, no decimals), but can
perform quite a number of different operations.
Spaces are not significant in arithmetic expressions and expressions may be nested.
For example, to multiply five squared by three:
Here is an example using the division and remainder operators. Notice the effect of
integer division:
Brace Expansion
Perhaps the strangest expansion is called brace expansion. With it, we can create
multiple text strings from a pattern containing braces. Here's an example:
Patterns to be brace expanded may contain a leading portion called a preamble and a
trailing portion called a postscript. The brace expression itself may contain either a
comma-separated list of strings, or a range of integers or single characters. The
pattern may not contain embedded whitespace. Here is an example using a range of
integers:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ echo {Z..A}
Z Y X W V U T S R Q P O N M L K J I H G F E D C B A
So what is this good for? The most common application is to make lists of files or
directories to be created. For example, if we were a photographer and had a large
collection of images we wanted to organize into years and months, the first thing we
might do is create a series of directories named in numeric “Year-Month” format. This
way, the directory names will sort in chronological order. we could type out a complete
list of directories, but that's a lot of work and it's error-prone too. Instead, we could
do this:
Pretty slick!
Parameter Expansion
We're only going to touch briefly on parameter expansion in this lesson, but we'll be
covering it more later. It's a feature that is more useful in shell scripts than directly on
the command line. Many of its capabilities have to do with the system's ability to store
small chunks of data and to give each chunk a name. Many such chunks, more
properly called variables, are available for our examination. For example, the variable
named “USER” contains our user name. To invoke parameter expansion and reveal the
contents of USER we would do this:
With other types of expansion, if we mistype a pattern, the expansion will not take
place and the echo command will simply display the mistyped pattern. With parameter
expansion, if we misspell the name of a variable, the expansion will still take place,
but will result in an empty string:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ echo $SUER
[me@linuxbox ~]$
Command Substitution
Command substitution allows us to use the output of a command as an expansion:
In this example, the results of the pipeline became the argument list of the file
command. There is an alternate syntax for command substitution in older shell
programs which is also supported in bash. It uses back-quotes instead of the dollar
sign and parentheses:
Quoting
Now that we've seen how many ways the shell can perform expansions, it's time to
learn how we can control it. Take for example:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ echo this is a test
this is a test
or:
In the first example, word-splitting by the shell removed extra whitespace from the
echo command's list of arguments. In the second example, parameter expansion
substituted an empty string for the value of “$1” because it was an undefined variable.
The shell provides a mechanism called quoting to selectively suppress unwanted
expansions.
Double Quotes
The first type of quoting we will look at is double quotes. If we place text inside double
quotes, all the special characters used by the shell lose their special meaning and are
treated as ordinary characters. The exceptions are “$”, “\” (backslash), and “`” (back-
quote). This means that word-splitting, pathname expansion, tilde expansion, and
brace expansion are suppressed, but parameter expansion, arithmetic expansion, and
command substitution are still carried out. Using double quotes, we can cope with
filenames containing embedded spaces. Imagine we were the unfortunate victim of a
file called two words.txt. If we tried to use this on the command line, word-splitting
would cause this to be treated as two separate arguments rather than the desired
single argument:
By using double quotes, we can stop the word-splitting and get the desired result;
further, we can even repair the damage:
There! Now we don't have to keep typing those pesky double quotes. Remember,
parameter expansion, arithmetic expansion, and command substitution still take place
within double quotes:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
17 18 19 20 21 22 23
24 25 26 27 28 29
By default, word-splitting looks for the presence of spaces, tabs, and newlines
(linefeed characters) and treats them as delimiters between words. This means that
unquoted spaces, tabs, and newlines are not considered to be part of the text. They
only serve as separators. Since they separate the words into different arguments, our
example command line contains a command followed by four distinct arguments. If we
add double quotes:
word-splitting is suppressed and the embedded spaces are not treated as delimiters,
rather they become part of the argument. Once the double quotes are added, our
command line contains a command followed by a single argument. The fact that
newlines are considered delimiters by the word-splitting mechanism causes an
interesting, albeit subtle, effect on command substitution. Consider the following:
In the first instance, the unquoted command substitution resulted in a command line
containing thirty-eight arguments. In the second, a command line with one argument
that includes the embedded spaces and newlines.
Single Quotes
When we need to suppress all expansions, we use single quotes. Here is a comparison
of unquoted, double quotes, and single quotes:
[me@linuxbox me]$ echo text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER
text /home/me/ls-output.txt a b foo 4 me
[me@linuxbox me]$ echo "text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER"
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
text ~/*.txt {a,b} foo 4 me
[me@linuxbox me]$ echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER'
text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER
As we can see, with each succeeding level of quoting, more and more of the
expansions are suppressed.
Escaping Characters
Sometimes we only want to quote a single character. To do this, we can precede a
character with a backslash, which in this context is called the escape character. Often
this is done inside double quotes to selectively prevent an expansion:
[me@linuxbox me]$ echo "The balance for user $USER is: \$5.00"
The balance for user me is: $5.00
To allow a backslash character to appear, escape it by typing “\\”. Note that within
single quotes, the backslash loses its special meaning and is treated as an ordinary
character.
ls -r
ls --reverse
Why do they support both? The short form is for lazy typists on the command line and
the long form is mostly for scripts though some options may only be available in long
form. Sometimes it is better to use a long option when the option is obscure or we
want to document more clearly what an option is. This is especially useful when
writing scripts where maximum readability is desired, and besides, anytime we can
save ourselves a trip to the man page is a good thing.
As we might suspect, using the long form options can make a single command line
very long. To combat this problem, we can use a backslash to get the shell to ignore a
newline character like this:
ls -l \
--reverse \
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
--human-readable \
--full-time
Using the backslash in this way allows us to embed newlines in our command. Note
that for this trick to work, the newline must be typed immediately after the backslash.
If we put a space after the backslash, the space will be ignored, not the newline.
Backslashes are also used to insert special characters into our text. These are called
backslash escape characters. Here are the common ones:
The use of the backslash escape characters is very common. This idea first appeared
in the C programming language. Today, the shell, C++, Perl, python, awk, tcl, and
many other programming languages use this concept. Using the echo command with
the -e option will allow us to demonstrate:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next
Permissions
The Unix-like operating systems, such as Linux differ from other computing systems in
that they are not only multitasking but also multi-user.
What exactly does this mean? It means that more than one user can be operating the
computer at the same time. While a desktop or laptop computer only has one
keyboard and monitor, it can still be used by more than one user. For example, if the
computer is attached to a network, or the Internet, remote users can log in via ssh
(secure shell) and operate the computer. In fact, remote users can execute graphical
applications and have the output displayed on a remote computer. The X Window
system supports this.
The multi-user capability of Unix-like systems is a feature that is deeply ingrained into
the design of the operating system. If we remember the environment in which Unix
was created, this makes perfect sense. Years ago before computers were "personal,"
they were large, expensive, and centralized. A typical university computer system
consisted of a large mainframe computer located in some building on campus and
terminals were located throughout the campus, each connected to the large central
computer. The computer would support many users at the same time.
In order to make this practical, a method had to be devised to protect the users from
each other. After all, we wouldn't want the actions of one user to crash the computer,
nor would we allow one user to interfere with the files belonging to another user.
File Permissions
On a Linux system, each file and directory is assigned access rights for the owner of
the file, the members of a group of related users, and everybody else. Rights can be
assigned to read a file, to write a file, and to execute a file (i.e., run the file as a
program).
To see the permission settings for a file, we can use the ls command. As an example,
we will look at the bash program which is located in the /bin directory:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Here we can see:
In the diagram below, we see how the first portion of the listing is interpreted. It
consists of a character indicating the file type, followed by three sets of three
characters that convey the reading, writing and execution permission for the owner,
group, and everybody else.
chmod
The chmod command is used to change the permissions of a file or directory. To use it,
we specify the desired permission settings and the file or files that we wish to modify.
There are two ways to specify the permissions. In this lesson we will focus on one of
these, called the octal notation method.
It is easy to think of the permission settings as a series of bits (which is how the
computer thinks about them). Here's how it works:
and so on...
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
rwx = 111 in binary = 7
rw- = 110 in binary = 6
r-x = 101 in binary = 5
r-- = 100 in binary = 4
Now, if we represent each of the three sets of permissions (owner, group, and other)
as a single digit, we have a pretty convenient way of expressing the possible
permissions settings. For example, if we wanted to set some_file to have read and
write permission for the owner, but wanted to keep the file private from others, we
would:
Here is a table of numbers that covers all the common settings. The ones beginning
with "7" are used with programs (since they enable execution) and the rest are for
other kinds of files.
Value Meaning
755 (rwxr-xr-x) The file's owner may read, write, and execute the file. All
others may read and execute the file. This setting is common for programs
that are used by all users.
700 (rwx------) The file's owner may read, write, and execute the file. Nobody
else has any rights. This setting is useful for programs that only the owner
may use and must be kept private from others.
666 (rw-rw-rw-) All users may read and write the file.
644 (rw-r--r--) The owner may read and write a file, while all others may only
read the file. A common setting for data files that everybody may read,
but only the owner may change.
600 (rw-------) The owner may read and write a file. All others have no rights.
A common setting for data files that the owner wants to keep private.
Directory Permissions
The chmod command can also be used to control the access permissions for
directories. Again, we can use the octal notation to set permissions, but the meaning
of the r, w, and x attributes is different:
r - Allows the contents of the directory to be listed if the x attribute is also set.
w - Allows files within the directory to be created, deleted, or renamed if the x
attribute is also set.
x - Allows a directory to be entered (i.e. cd dir).
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Here are some useful settings for directories:
Value Meaning
755 (rwxr-xr-x) The directory owner has full access. All others may list the
directory, but cannot create files nor delete them. This setting is common
for directories that you wish to share with other users.
700 (rwx------) The directory owner has full access. Nobody else has any
rights. This setting is useful for directories that only the owner may use
and must be kept private from others.
[me@linuxbox me]$ su
Password:
[root@linuxbox me]#
After executing the su command, we have a new shell session as the superuser. To
exit the superuser session, type exit and we will return to your previous session.
In most modern distributions, an alternate method is used. Rather than using su,
these systems employ the sudo command instead. With sudo, one or more users are
granted superuser privileges on an as needed basis. To execute a command as the
superuser, the desired command is simply preceded with the sudo command. After the
command is entered, the user is prompted for the their own password rather than the
superuser's:
In fact, modern distributions don't even set the root account password thus making it
impossible to log in as the root user. A root shell is still possible with sudo by using the
"-i" option:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$ sudo -i
Password for me:
root@linuxbox:~#
Notice that in order to change the owner of a file, we must have superuser privileges.
To do this, our example employed the sudo command to execute chown.
In the example above, we changed the group ownership of some_file from its
previous group to "new_group". We must be the owner of the file or directory to
perform a chgrp.
Further Reading
Chapter 9 of The Linux Command Line covers this topic in much more detail.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Previous | Contents | Next
Job Control
In the previous lesson, we looked at some of the implications of Linux being a multi-
user operating system. In this lesson, we will examine the multitasking nature of
Linux, and how it is controlled with the command line interface.
There are several commands that are used to control processes. They are:
A Practical Example
While it may seem that this subject is rather obscure, it can be very practical for the
average user who mostly works with the graphical user interface. Though it might not
be apparent, most (if not all) graphical programs can be launched from the command
line. Here's an example: there is a small program supplied with the X Window system
called xload which displays a graph representing system load. We can execute this
program by typing the following:
Notice that the small xload window appears and begins to display the system load
graph. On systems where xload is not available, try gedit instead. Notice also that
our prompt did not reappear after the program launched. The shell is waiting for the
program to finish before control returns. If we close the xload window, the xload
program terminates and the prompt returns.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[me@linuxbox me]$
In this case, the prompt returned because the process was put in the background.
Now imagine that we forgot to use the "&" symbol to put the program into the
background. There is still hope. We can type Ctrl-z and the process will be
suspended. We can verify this by seeing that the program's window is frozen. The
process still exists, but is idle. To resume the process in the background, type the bg
command (short for background). Here is an example:
[me@linuxbox me]$ bg
[2]+ xload &
[me@linuxbox me]$ ps
PID TTY TIME CMD
1211 pts/4 00:00:00 bash
1246 pts/4 00:00:00 xload
1247 pts/4 00:00:00 ps
[me@linuxbox me]$
Killing a Process
Suppose that we have a program that becomes unresponsive; how do we get rid of it?
We use the kill command, of course. Let's try this out on xload. First, we need to
identify the process we want to kill. We can use either jobs or ps, to do this. If we use
jobs we will get back a job number. With ps, we are given a process id (PID). We will
do it both ways:
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
[2] 1293
[1] Terminated xload
[me@linuxbox me]$ ps
PID TTY TIME CMD
1280 pts/5 00:00:00 bash
1293 pts/5 00:00:00 xload
1294 pts/5 00:00:00 ps
[me@linuxbox me]$
kill -l
will print a list of the signals it supports. Many are rather obscure, but several are
handy to know:
1 SIGHUP Hang up signal. Programs can listen for this signal and act
upon it. This signal is sent to processes running in a
terminal when you close the terminal.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
this signal.
Now let's suppose that we have a program that is hopelessly hung and we want to get
rid of it. Here's what we do:
1. Use the ps command to get the process id (PID) of the process we want to
terminate.
2. Issue a kill command for that PID.
3. If the process refuses to terminate (i.e., it is ignoring the signal), send
increasingly harsh signals until it does terminate.
In the example above we used the ps command with the x option to list all of our
processes (even those not launched from the current terminal). In addition, we piped
the output of the ps command into grep to list only list the program we are interested
in. Next, we used kill to issue a SIGTERM signal to the troublesome program. In
actual practice, it is more common to do it in the following way since the default signal
sent by kill is SIGTERM and kill can also use the signal number instead of the
signal name:
Then, if the process does not terminate, force it with the SIGKILL signal:
That's It!
This concludes the "Learning the Shell" series of lessons. In the next series, "Writing
Shell Scripts," we will look at how to automate tasks with the shell.
Further Reading
For a more in-depth treatment of the topic, see Chapter 10 in The Linux
Command Line.
1963 Timesharing: A Solution to Computer Bottlenecks, a fascinating YouTube
video from the Computer History Museum describing the first timesharing
operating system and how it works. It's basically the same method used by all
modern computers.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com