Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Introduction to BASH

WHERE TO START

What is a shell?
Whenever you type a command on the black screen and hit RETURN, a program interprets the
line that you have entered, translates it to machine language understood by the computer to
execute it. If the command produces a result, that result is conveyed back to the user (for
example ls l will output the file listing of the current directory)

The interface that accepts user input and produces output is called a shell.
BASH belongs to character-based user interface or CUI. There are other types of interfaces like
the GUI (Graphical User Interface), which may accept not only text based input, but also mouse
(or other pointing device) movements, or even finger touches and gestures like in smartphones
and ATM screens.
The shell interprets text by breaking it into smaller parts. For example: ls l /etc command
is interpreted as ls, the command, -l, the first argument, and /etc, the second argument
BASH can operate in two modes: the interactive mode (writing commands directly to the shell),
and programming mode (writing shell scripts)

BASH origins
Because a shell is an independent program of the underlying operating system (UNIX or Linux),
a large number of shells were created since the introduction of UNIX.
The most popular one was the Bourne shell, created by Steven Bourne. It was included in the
very first editions of UNIX. It is still used and popular till now (recognized as sh on the system)

Then came the c-shell (csh). It bears its name from the resemblance between its commands and
the ones used in the c programming language, which was aimed at making it easier for c
programmers to learn UNIX shells. Korn shell (ksh) was also introduced. It has the best features
of both Bourne and C shells.
The Bourne-Again shell (BASH), which is considered a successor the original Bourne shell was
introduced to be the default shell of GNU products. GNU (short for GNU Not UNIX) is a project
that aimed at providing free alternatives to the commercial UNIX operating system. The most
important product was Linux.

Why BASH?
BASH is not the only free OS shell available for Linux and UNIX, we have Korn shell and c shell.
However, BASH has the following features that makes it a popular choice among other shells:
It is compatible with the Bourne shell so if you are coming from sh world youll feel at home
It incorporates a lot of features from Korn and C shells
It has command-line editing modes that let going back and fixing typos in previous commands much
easier than the C shell history mechanism, let alone the Borne shell that does not have this feature at
the first place
The job control feature, which makes you start, suspend, or stop any number of commands at the
same time.
It offers a lot of customization options and programming features that makes it well suited for
programmers as well as system administrators.

Whats my current shell?


You may or may not be using BASH as your current system shell. This depends on your
customization or the default shell that your system administrator (if it is not you) has configured
for users.
You can quickly determine your current shell by typing echo $SHELL at the command
prompt. The response should be like: ksh, bash, or csh, denoting Korn, BASH, or C shells. It
may also bring something like tcsh or zsh which are also types of shells.
If you are not running BASH, you can try to use it just by typing bash at the command line. If
the command succeeded then you are now using BASH. However, if the shell is not installed on
your system you will see an output like bash command not found.
Another option is to examine your username in /etc/passwd and see which shell was
configured for you. For example: grep ahmed /etc/passwd | cut -d : -f 7 just
replace ahmed with your username. The output should be the location of the current shell like
/bin/ksh if the configured shell is Korn.

The interactive mode


In interactive mode, you feed your commands to the shell one at a time.
Each command consists of one or more words separated by spaces or TAB characters. Each
command should end with a line feed (hit RETURN or ENTER button) to be interpreted by the
shell.

Each command may have one or more arguments (or parameters) which control the behavior or
scope of the command. For example cat /etc/hosts consists of the command cat, which is
used to output the contents of a file to the screen, followed by the path to the file as an
argument.
Options are command arguments that control the behavior of the command. For example ls
/etc/hosts will just list the file /etc/hosts, which does not provide much benefit. But
adding l option to the command to be ls l /etc/hosts will provide some metadata
about the file like the permissions, the owner user and group, and last modification date.

Everything is a file
You will often read and hear this phrase when working with UNIX based systems. UNIX treats
everything on the system as a file. That includes ASCII and binary files as well as devices.
The following are some types of files on UNIX:
Regular: this refers to text files. Any files that contain human readable text
Executable: programs that get executed against the system. Such files may contain human-readable text
(like in shell scripts) or they can be binary files containing non human-readable data (like /bin/ls)
Directories: yes UNIX views directories as special types of files that contain other files (or directories).

The directory concept


Since a directory can be the container of other directories on a give system, this creates a hierarchy of files and
directories that look like an inverted tree.
If a tree gets inverted, the root is at the top. Its denoted by the slash character /. Any files or directories must
have its path flowing from root, and separated by slashes. This method is called absolute path.
The directory where you are currently standing is called working directory. For example, is you type cd
/home/joe, you are currently in the directory joe located under directory /home.
If you want to access directory /home/joe/pictures/family, and if your working directory is
/home/joe, then you can navigate to it just by typing cd pictures/family. In this case, you have
navigated to the relative path.

The initial directory that the user is put in upon logging to the system is called the home directory. BASH let you
refer to the home directory by the special character tilde ~. So if you type cd ~/pictures/family, you
are referring to /home/joe/pictures/family. You can also use the tilde notation to refer to other users
home directories. For example ~frank refers to /home/frank
You can use cd to change your working directory by specifying the relative or absolute path. The dot (.) refers to
the current directory, the double dots (..) refers to the parent directory and using cd without arguments changes
the current directory to the home directory.

Listing files
The ls command is used to list files. It takes the path as an argument (can take more than one path),
and if called without arguments, it displays the files and directories in the current directory.
The ls command is often used with arguments to make it useful. For example: -l for long listing
provides more information about the listed files, -a lists also the hidden files and directories (any file
that stats with a dot . is not shown by default).

You use wildcards with ls to display only files that follow a certain criteria:
* Any number of characters
? A single character
[] a set of characters. This may by just a number of single characters like [abc] which will match a, or b, or c, or
a range of characters like [a-z], which will match all lowercase letters, or a combination like [a-zA-Z], which will
match all uppercase and lowercase letters.
[!set] adding the exclamation mark before the set negates it. That is, it matches any occurrence that do not
follow the pattern in the set. For example: [!abc] will match any character that is not a, b, or c
You can always precede a character with backslash \ to escape it. That is, match the literal charcacter. For
example [\!abc] will match any character that is !, a, b, or c.

Standard Input/Output
Any program that is invoked through the shell has at least three pipes: standard input (from
which it accepts its data), standard output (to which the results are passed), and standard error
(to which error messages are directed). For example, in the ls command example, the command
takes its standard input from the keyboard (the path) and prints the standard output and the
standard error to the standard output, which is the screen.
It is possible to direct any of the three channels to a file. For example, the ls command accepts
its arguments (the path) from the standard input, which is the keyboard. While the sort
command takes its input as a redirection from a file. For example sort < items.txt

UNIX allows you to direct the standard output of a command to the standard input of another.
This is used massively in filtering commands like grep, sort, cut and others. For example ps
ef | grep httpd will list all the running processes on the system (output of ps ef), then
this output is fed to the grep command as its standard input using the pipe character |. The
grep command will only display lines that contain the word httpd, effectively informing the
user with any running Apache webserver processes.

Running jobs in the background


Whenever you type a command or a execute a program on the shell, it takes control of this shell,
denying you from entering other commands until it finishes.
If you are executing a program that will take a long time to finish, you can either open another
session to the system, or add the ampersand & character at the end of the command line to
make this command run in the background. Once sent to the background, the shell will refer to
it with by a number called job number and will be printed immediately to the screen.
Background jobs are closely related to daemons. That is, programs or services that will always
be running in the background without the need of user input. For example, the httpd webserver.

While running in the background mode, programs will still print any text that is supposed to go
to the standard output to the screen, along with any standard error messages. This behavior can
be avoided by directing the output to a file with > character. For example
./backgroundjob.sh > logfile.txt &

Quoting
There is a number of characters that bear special meaning in the shell interactive mode. Weve
already seen the ~ * ? | < > &. But there are other symbols like ( ) [ ] { } and others (covered in later
sections)
When you enter a command that contains one of those characters, you may receive unpredictable
results. Accordingly, to make it work as expected, you have to quote any strings that contains one or
more of those characters by single quotes. For example, you want to create a directory named
#trending, in which you are going to store trending topics from Twitter. If you issued the command
mkdir #trending, the shell will interpret the your command as mkdir only because the #
symbol is used for comments. That is, any string that comes after # is ignored. To create the directory
without losing the # character youd issue the command like this: mkdir '#trending'.
Another way of quoting special characters is preceding them with backslah \. You can escape single
and double quotes like \' and \" or escape the backslash itself like this \\
You can even quote the RETURN character (the line feed) to write an excessively long piece of text
that spans multiple lines, by adding \ at the end of the line and pressing RETURN, or by pressing
RETURN while already inside a quoted string (like between ')

Control commands
These are special instructions sent to the system using the CTRL key in combination with
another character. The following are some of the common combinations and there
interpretations:
CTRL-C: exit from the current running command. If you run it while in writing a command, the shell will
skip it and start a new line immediately.
CTRL-D: end of input. Used with commands that expect output from a file like sort
CTRL-U: erases the current line
CTRL-Z: suspends the current running command

Finding help
BASH is the only shell that provides an online help system to its commands.
If you type help followed by the command name, detailed usage and description will be
displayed. For example help pwd
Note that the help command usage applies to only BASH commands. That is, the built-in shell
commands and not other third-party commands. For example, you cannot run help ssh
The help command can also accept partial command names. For example help ex will match
exec and export

If help outputs a large amount of text that scrolls multiple screens, you can pipe it to a
command like more or less
For other non built-in commands, you can use the man command. For example, man ls

You might also like