Ph.D. in Computer Science at Rutgers University

Bash Basic

Shell is an UNIX term for a user interface to the system which let you interact with the Operating System via keyboard and display. There're a lot of shells available like Korn Shell, C-Shell. Bourne Again Shell is one of the most popular shell in UNIX and Linux distros. This post will start with Bash basics to introduce how to use BASH.

Command, Arguments and Options

As we know, in UNIX or Linux, command is one of the basic element of Shell. There's hundreds of built-in commands in Linux. A command typically has arguments and options. Here's an example.

cp -r /etc/ ./

This example shows the cp command with -r option and two arguments "/etc/" and "./". This command copy whatever in directory "/etc/" to the current directory recursively.

File, Directory and Access Control List

In UNIX and Linux, files and directories are organized as a tree-like structure (or DAG). And file is a frequent used argument in commands. There're three types of files that are most important by far.

Regular Files: Also known as text files containing readable characters.

Excutable Files: Also known as programs or scripts; these're invoked as commands. Some cannot be read by humans - ELF executable files; Some could be read by human - Bash script or python script.

Directories: Directories are like folders that contain other files or other directories.

Access Control List (ACL) provides an additional, more flexible permission mechanism for file systems. It is designed to assist with UNIX file permissions. ACL allows you to give permissions for any user or group to any disc resource. The access control list has totally 10 bits. 3-bits as a set for read, write and execute respectively. 3 sets for file owner, group and other. And an addition set of sticky bit indicate it's a directory or it has setuid priviledge. Here's an example of file ACL in Linux:

-rw-------  1 ryj  ryj  30204 Oct 14 13:04 .bash_history
-rw-r--r--  1 ryj  ryj    220 Mar  8  2017 .bash_logout
-rw-r--r--  1 ryj  ryj   3771 Mar  8  2017 .bashrc
drwxr-xr-x  2 ryj  ryj   4096 Mar  8  2017 Desktop/
drwxr-xr-x  8 ryj  ryj   4096 Sep 11 18:41 Documents/
drwxr-xr-x  8 ryj  ryj   4096 Oct  8 11:24 Downloads/

In this example, The Desktop directory has 755 permission "rwxr-xr-x", owner is ryj, group is ryj.

Standard I/O and I/O redirection

Each UNIX program has three basic input and output: stdin, stdout and stderr. All shells handle standard I/O in basically the same way. Each program that you invoke has all three starnd I/O channels set to your terminal or workstation, so standard input is your keyboard and standard ouput and standard error are your screen or window.

I/O redirection allows you redirect standard input so that it comes from files. The input redirection by using "<" and output redirection by using ">". Here's an example:

cat < file1 > file2

The cat command concatenate standard input to standard output. In this example, standard input is redirected to file1 and standard output is redirected to file2. So this command basically copies whatever in file1 to file2

Special Characters and Quoting in Bash

In bash, there're some special characters have particular meaning to the shell. I/O redirection characters "<" and ">" are two of the special characters. The table below shows the special characters in bash and their usage:


Sometimes you want to use special characters literally. This is called quoting. There're two ways to quote a special character. One is using single quotes ', and another is using backslash-escaping. Here's an example:

echo 2 * 3 > 5 is a valid inequality

The result of this command will not print on console. Instead there will be new file named 5 whose content is 2 following names of all files in current directory and then the string 3 is a valid inquality. Because without quoting to special characters, * and > will have special meaning. We could fix by the following:

echo '2 * 3 > 5' is a valid inequality    # Method 1
echo 2 \* 3 \> 5 is a valid inequality    # Method 2

If you want to quote quotation marks, just use backslash.