By: Ayokunle Oladapo
July 31, 2020
A Beginner's Guide to the Bash Terminal
By: Ayokunle Oladapo
July 31, 2020
What is Bash?
Bash (Bourne Again Shell) is the command language interpreter or shell for the GNU/Linux operating system. It was named after Stephen Bourne, who was the author of the Bourne shell (sh).
The Bash shell can be found on most GNU/Linux systems and, recently, on most Unix systems.
What is a Shell?
A shell is simply a program that allows a computer user to run other programs and commands with input from the keyboard or a shell script in a Unix or GNU/Linux operating system. It is a command language interpreter for these OSes.
Previously, it was the only interface available to a user in an operating system like Linux.
There are different types of shell programs, including the Bourne Shell, C Shell, TENEX C Shell, Korn Shell, Bash Shell, Fish Shell, and the Z Shell.
As indicated in the title, this article will focus solely on the Bash Shell.
The prompt, $, which indicates the command prompt of a shell, allows the user to type in a command which is then executed after the Enter key on the keyboard is pressed. The command to run is determined by the first word (unbroken without spaces or tabs) written after the prompt, $.
NB: The shell prompt becomes a hashtag (#) if the logged-in user is root.
A terminal is generally used to interact with a shell program.
What is a Terminal?
A terminal or terminal emulator provides the computer user with an interface to access the command line. In this interface, text-based commands can be typed and executed. In short, it opens a window to allow a user to interact with the shell.
When a terminal window is opened, a shell prompt is displayed, ready to accept commands.
A Bash Terminal is a terminal that has been set to use the Bash Shell.
Shell scripts are commands that are in the order in which they are to be executed. They are text files that use the .sh extension and are used to perform operations involving file manipulation, program execution, and text printing.
Introduction to Bash Terminal
This article will go over just a few of the many things that the bash terminal can do.
Whenever a bash terminal is opened, the username@hostname followed by the dollar sign, $, is seen, as illustrated below,
Commands can be typed into the shell after the dollar sign ($).
The username@hostname$ displayed can be customized as needed by the user to display information such as date, time, or less information. This can be achieved by changing the PS1 value in the ~/.bashrc file. The PS1 value is the primary prompt that is shown before each command.
There are also PS2 (displayed when a command needs more input), PS3 (for Bash’s select built-in keyword), and PS4 (displayed when debugging bash scripts).
There are various escape characters - e.g., \u for current username and \A for the current time - used to set what is displayed before each command. Here, the value of PS1 will be illustrated for changing PS1 to a string value.
It is recommended to backup the ~/.bashrc file before editing.
To backup the file, type the following command.
As seen above, the illustration of using the bash cp command has been shown, along with using bash to execute another program, vi, which was used to edit the ~/.bashrc file. Here are more common commands.
The man command is used to view the manuals for the command or program that is given. It shows a detailed description of options, arguments, usage of a command. Usage: man [options][command] example:
cat The cat command is used to read the contents of a file in sequence and display it on the standard output. Usage: cat [options] [file_name] Example:
The working directory will be changed to the Desktop, a subdirectory of the present working directory. The cd command can also be used to change to directories outside of the working directory.
cd / will change the working directory to the file system directory, cd ~ will go to the logged-in user directory, and cd .. is used to navigate up a directory.
mv The mv command is used to move files and folders. Usage: mv [options] [source_path] [destination_path] or: mv [options]... source... directory or: mv [options]... -t directory source...
touch To create a new file, the touch command is used. Usage: touch [options] [newfilename] Example:
This creates a new file named new_file in the pwd.
rm rm is used to remove or delete a file. Usage: rm [options] [ filename] Example:
echo The echo command is used to print a string to the standard output or a file. Usage : echo “Hello world” – prints “Hello world” to the terminal echo “Hello world” > filename – prints “Hello World” to the file with name “filename” if the file exists or it will create the file if it doesn’t exist.
Apart from just typing in commands, commands can be written in a script and used as an input to the bash shell. The script is a text file with a .sh extension. Taking a look at a simple bash script:
Let’s say this script is saved as simple.sh in the home directory, and a terminal is opened to the same directory. The script can be executed as follows: $./simple.sh
The first line of the script #! /bin/bash is often called “shebang”, “shabang,” or many other names. It lets the system know what interpreter to use. Note that it begins with #! - The shell treats this in a special way.
The second line starts with a #. This is used to make comments.
The third line then prints the quoted text, without the quotes, to the terminal using the echo command.
The fourth line accepts an input from the user using the read command, saving the input in a variable named RESPONSE.
The fifth line then prints the content of the variable RESPONSE to the standard output. The $ is used behind the variable name, as in $RESPONSE, to access the content of that variable.
Shell scripts can use variables, conditions, and loops and perform arithmetic operations, comparisons, and more complex operations, all telling the shell environment when and what to do.
There are many advantages to using a shell. An outstanding advantage is the ability to use shell scripts to automate numerous manual processes and gain better control of the operating system than the GUI naturally permits.
As long as this guide is, it is still the tip of the iceberg when it comes to bash scripting. Consulting more detailed material, coupled with practice, will pave the road to becoming an expert in shell scripting.