02-01-2013, 10:01 AM
Bash Guide for Beginners
1Bash Guide.pdf (Size: 1.14 MB / Downloads: 20)
Introduction
Why this guide?
The primary reason for writing this document is that a lot of readers feel the existing HOWTO to be too short
and incomplete, while the Bash Scripting guide is too much of a reference work. There is nothing in between
these two extremes. I also wrote this guide on the general principal that not enough free basic courses are
available, though they should be.
This is a practical guide which, while not always being too serious, tries to give real-life instead of theoretical
examples. I partly wrote it because I don't get excited with stripped down and over-simplified examples
written by people who know what they are talking about, showing some really cool Bash feature so much out
of its context that you cannot ever use it in practical circumstances. You can read that sort of stuff after
finishing this book, which contains exercises and examples that will help you survive in the real world.
From my experience as UNIX/Linux user, system administrator and trainer, I know that people can have years
of daily interaction with their systems, without having the slightest knowledge of task automation. Thus they
often think that UNIX is not userfriendly, and even worse, they get the impression that it is slow and
old-fashioned. This problem is another one that can be remedied by this guide.
Who should read this book?
Everybody working on a UNIX or UNIX-like system who wants to make life easier on themselves, power
users and sysadmins alike, can benefit from reading this book. Readers who already have a grasp of working
the system using the command line will learn the ins and outs of shell scripting that ease execution of daily
tasks. System administration relies a great deal on shell scripting; common tasks are often automated using
simple scripts. This document is full of examples that will encourage you to write your own and that will
inspire you to improve on existing scripts.
Contributions
Thanks to all the friends who helped (or tried to) and to my husband; your encouraging words made this work
possible. Thanks to all the people who submitted bug reports, examples and remarks - among many, many
others:
· Hans Bol, one of the groupies
· Mike Sim, remarks on style
· Dan Richter, for array examples
· Gerg Ferguson, for ideas on the title
· Mendel Leo Cooper, for making room
· #linux.be, for keeping my feet on the ground
Common shell programs
General shell functions
The UNIX shell program interprets user commands, which are either directly entered by the user, or which
can be read from a file called the shell script or shell program. Shell scripts are interpreted, not compiled. The
shell reads commands from the script line per line and searches for those commands on the system (see
Section 1.2), while a compiler converts a program into machine readable form, an executable file - which may
then be used in a shell script.
Apart from passing commands to the kernel, the main task of a shell is providing a user environment, which
can be configured individually using shell resource configuration files.
Advantages of the Bourne Again SHell
Bash is the GNU shell
The GNU project (GNU's Not UNIX) provides tools for UNIX-like system administration which are free
software and comply to UNIX standards.
Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It
is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional
improvements over sh for both programming and interactive use; these include command line editing,
unlimited size command history, job control, shell functions and aliases, indexed arrays of unlimited size, and
integer arithmetic in any base from two to sixty-four. Bash can run most sh scripts without modification.
Like the other GNU projects, the bash initiative was started to preserve, protect and promote the freedom to
use, study, copy, modify and redistribute software. It is generally known that such conditions stimulate
creativity. This was also the case with the bash program, which has a lot of extra features that other shells
can't offer.
Conditionals
Conditional expressions are used by the [[ compound command and by the test and [ built-in commands.
Expressions may be unary or binary. Unary expressions are often used to examine the status of a file. You
only need one object, for instance a file, to do the operation on.
There are string operators and numeric comparison operators as well; these are binary operators, requiring two
objects to do the operation on. If the FILE argument to one of the primaries is in the form /dev/fd/N, then
file descriptor N is checked. If the FILE argument to one of the primaries is one of /dev/stdin,
/dev/stdout or /dev/stderr, then file descriptor 0, 1 or 2 respectively is checked.
Aliases
Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The
shell maintains a list of aliases that may be set and unset with the alias and unalias commands.
Bash always reads at least one complete line of input before executing any of the commands on that line.
Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition
appearing on the same line as another command does not take effect until the next line of input is read. The
commands following the alias definition on that line are not affected by the new alias.
Aliases are expanded when a function definition is read, not when the function is executed, because a function
definition is itself a compound command. As a consequence, aliases defined in a function are not available
until after that function is executed.
Directory stack
The directory stack is a list of recently-visited directories. The pushd built-in adds directories to the stack as it
changes the current directory, and the popd built-in removes specified directories from the stack and changes
the current directory to the directory removed.
Content can be displayed issuing the dirs command or by checking the content of the DIRSTACK variable.
More information about the workings of this mechanism can be found in the Bash info pages.
Executing commands
General
Bash determines the type of program that is to be executed. Normal programs are system commands that exist
in compiled form on your system. When such a program is executed, a new process is created because Bash
makes an exact copy of itself. This child process has the same environment as its parent, only the process ID
number is different. This procedure is called forking.
After the forking process, the address space of the child process is overwritten with the new process data. This
is done through an exec call to the system.
The fork-and-exec mechanism thus switches an old command with a new, while the environment in which the
new program is executed remains the same, including configuration of input and output devices, environment
variables and priority. This mechanism is used to create all UNIX processes, so it also applies to the Linux
operating system. Even the first process, init, with process ID 1, is forked during the boot procedure in the
so-called bootstrapping procedure.
Executing programs from a script
When the program being executed is a shell script, bash will create a new bash process using a fork. This
subshell reads the lines from the shell script one line at a time. Commands on each line are read, interpreted
and executed as if they would have come directly from the keyboard.
While the subshell processes each line of the script, the parent shell waits for its child process to finish. When
there are no more lines in the shell script to read, the subshell terminates. The parent shell awakes and
displays a new prompt.
Shell parameters
A parameter is an entity that stores values. It can be a name, a number or a special value. For the shell's
purpose, a variable is a parameter that stores a name. A variable has a value and zero or more attributes.
Variables are created with the declare shell built-in command.
If no value is given, a variable is assigned the null string. Variables can only be removed with the unset
built-in.
Executing commands
When executing a command, the words that the parser has marked as variable assignments (preceding the
command name) and redirections are saved for later reference. Words that are not variable assignments or
redirections are expanded; the first remaining word after expansion is taken to be the name of the command
and the rest are arguments to that command. Then redirections are performed, then strings assigned to
variables are expanded. If no command name results, variables will affect the current shell environment.