What is Bash

Bash (short for Bourne Again SHell) is a Unix shell, and a command language interpreter. A shell is simply a macro processor that executes commands. It's the most widely used shell packaged by default for most Linux distributions, and a successor for the Korn shell (ksh) and the C shell (csh).

Many things that can be done Linux operating system can be done via command line. Some examples are…

You can read more about Bash here, via the GNU Documentation, and via the tldp guide.

Using on the command line

You can start using bash on most Linux and OS X operating systems by opening up a terminal. Let's consider a simple hello world example. Open up your terminal, and write the following line (everything after the $ sign):

zach@marigold:~$ echo "Hello world!"

As you can see, we used the echo command to print the string “Hello world!” to the terminal.

Writing a bash script

You can also put all of your bash commands into a .sh file, and run them from the command line. Say you had a bash script with the following contents:

#!/bin/bash
echo "Hello world!"

It's worth noting that first line of the script starts with #!. It is a special directive which Unix treats differently.

Why did we use #!/bin/bash at the beginning of the script file?

That is because it is a convention to let the interactive shell know what kind of interpreter to run for the program that follows. The first line tells Unix that the file is to be executed by /bin/bash. This is the standard location of the Bourne shell on just about every Unix system. Adding #!/bin/bash as the first line of your script, tells the OS to invoke the specified shell to execute the commands that follow in the script. #!is often referred to as a “hash-bang”, “she-bang” or “sha-bang”.

Though it is only executed if you run your script as an executable. For example, when you type ./scriptname.extension, it will look at the top line to find out the interpreter, whereas, running the script as bash scriptname.sh, first line is ignored.

Then you could run the script like so:

zach@marigold:~$ ./myBashScript.sh
Hello world!

The script only has two lines. The first indicates what interpreter to use to run the file (in this case, bash). The second line is the command we want to use, echo, followed by what we want to print which is “Hello World”.

Sometimes the script won't be executed, and the above command will return an error. It is due to the permissions set on the file. To avoid that use:

zach@marigold:~$ chmod u+x myBashScript.sh

or

zach@marigold:~$ chmod 744 myBashScript.sh

And then execute the script.

Running commands as 'root'

Sometimes, running certain commands in bash will return in an error letting you know you don't have the proper permissions to execute it. This means the command requires elevated permissions to run. This can be achieved, through the "sudo" command. Users can be granted special "sudoer" permissions through a file located at /etc/sudoers. To run sudo, you simply need to prepend it to the front of the command you're running. It will then ask you for your password to validate that you're authorized to run this command as well as to verify that you are intending to run it as "root". Running a command as root means you are running it with the highest permissions available on the system with no restrictions.

An example of a sudo command might look something like this:

$sudo apt update
            [sudo] password for user:
            Get:1 http://deb.parrotsec.org/parrot parrot InRelease
            

Script Example

If you execute this script it is going to print out your name.

#!/usr/bin/env bash
NAME="John"  
echo  "Hello $NAME!"
            

cat

Cat is one of the most frequently used commands in Unix-like operating systems.

Cat is used to read a file sequentially and print it to the standard output. The name is derived from its function to concatenate files.

Usage

cat [options] [file_names]

Most used options:

Example

Print in terminal the content of file.txt:

cat file.txt

Concatenate the content of the two files and display the result in terminal:

cat file1.txt file2.txt

cd

Change Directory to the path specified, for example cd projects.

There are a few really helpful arguments to aid this:

ls

ls is a command on Unix-like operating systems to list contents of a directory, for example folder and file names. For each file named other than a directory, ls displays its name as well as any other information.

Usage

ls [options] [file_names]

You can list the items in any directory without even entering the directory. Consider you are in a directory with folders- Test1,Test2. You're in the parent directory you can list all files in Test1 as follows- ls Test1

Most used options:

Example

List files with details in home/user/docs

ls -la
total 4
-rwxrwx---    1 root root      5514 Feb  4  2018 log1.txt
-rwxrwx---    1 root root      1024 Feb  5  2018 colors.txt
-rwxrwx---    1 root root       112 Feb  8  2018 output.txt
-rwxrwx---    1 root root       514 Feb  9  2018 notes.txt

man

Man, the abbreviation of manual, is a bash command used to display on-line reference manuals of the given command.

Usage

man [options] [command]

Most used options:

Examples

Display the man page of ls

man ls

Display a short description of the shutdown command

man -f shutdown

Display the man page of ASCII table:

man ascii

mv

The mv command is used to move files and folders.

Usage

mv source_directory target_directory

The first argument is the file you want to move, and the second is the location to move it to. It can also be used to rename a file, by inputting 2 file names as parameters, like so: mv originalFilename.txt newFilename.txt

Commonly used options:

Warning

This command is capable of modifying many files at once with ease. This can be beneficial, but also dangerous. Use at your own risk.

More Information

Reference List