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…
- Editing Files
- Adjusting the volume of the operating system
- Fetching web pages from the internet
- Automating work you do everyday
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:
-b
, number non-blank output lines-n
, number all output lines-s
, squeeze multiple adjacent blank lines-v
, display nonprinting characters, except for tabs and the end of line character
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:
.
refers to the current directory, such as./projects
..
can be used to move up one folder, usecd ..
, and can be combined to move up multiple levels../../my_folder
/
is the root of your system to reach core folders, such assystem
,users
, etc.~
is the home directory, usually the path/users/username
. Move back to folders referenced relative to this path by including it at the start of your path, for example~/projects
.
head
The head command allows you to view the first N lines of a file. if more than on file is called then the first ten lines of each file is displayed, unless specific number of lines are specified. Choosing to display the file header is optional using the option below.
Usage
head [options] [file_name(s)]
Most used options:
-n N
, prints out he first N lines of the file(s)-q
, doesn't print out the file headers-v
, always prints out the file headers
Example
head file.txt
Prints the first ten lines of file.txt(default)
head -n 7 file.txt
Prints the first seven lines of file.txt
head -q -n 5 file1.txt file2.txt
Prints the first 5 lines of file1.txt, followed by the first 5 lines of file2.txt
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:
-a
, all files and folders, including ones that are hidden and start with a.
-d
, list directories themselves and not contents of directories-l
, list in long format-lh
, shows sizes in human readable format-lS
, displays file size in order; will display big in size first-G
, enable colorized output-s
, list file size-t
, sorts the output by modification time-r
, reverses the order while sorting-R
, displays the contents of the directory and its subdirectories
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:
-f
, print a short description of the given command-a
, display, in succession, all of the available intro manual pages contained within the manual-c
, reformat the source man page, used mostly when the page was formatted for a screen with a different number of columns-l
, enabling case sensitivity
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:
-b
to create a backup file with the name of the destination file-f
to force move them and overwrite files without checking with the user-i
to prompt confirmation before overwriting files-n
do not overwrite an existing file-T
to rename a file named source to destination
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.