Introduction

Bash is a scripting language that runs within the terminal on most Linux distros, as well as MacOS. Shell scripts are a sequence of bash commands within a file, combined together to achieve more complex tasks than simple one-liner and are especially useful when it comes to automating tasks.

History

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions.

Bash Basics

Man Pages (Bash Manual/Help)

The man pages in Bash refers to the manual or guide for usage of each binary used in Bash. These manual pages help us understand the basic and advance usage of the program we run: To use it, all we have to do is type man followed by the program we wish to learn more about:

man ping

NOTE: The majority of Bash programs will have man pages, but there are a few programs which use the -h or --help command like so:

ping -h

Bash Built-in Commands

To list the current directory we are in:

ls

# to list files in list view
ls -l

# to list hidden files
ls -a

To show our current working directory:

pwd

To move from current directory to another, we will use cd:

# from current directory to another
cd /opt/directoryName

# to move up one directory
cd ..

# to move up to specific number of directories
cd ../../../

To create a folder/directory:

mkdir nameMe

# to create multiple directories
mkdir -p dir1 dir2 dir3/insideDir4

Bash File Manipulation

To copy a file to another location:

cp filename.txt /path/to/location/

# example
cp passwords.txt /root/secretFolder

To copy entire folder with its contents:

cp -r folderName/ /path/to/location

# example
cp -r backFiles/ /opt/backupStorage

To move a file and/or folder:

mv filename.txt /path/to/move/to

mv folderName/ /path/to/move/to

To delete a file:

rm filename.txt

To delete a folder:

rm -rf folderName/

To print something to output:

echo "This is sentence within the quotes will output in commndline"

Reading Files

To output contents of a file:

cat filename.txt

To output into a program instead of the command line:

more filename.txt
# or
less filename.txt

To get the first or last few lines of a file:

# first 10 lines 
head filename.txt

# last 10 lines
tail filename.txt

Bash Environment Variables

An environment variable is a dynamically named value that can affect the running processes will behave on a computer. In other words, they are part of the environment in which a process runs. We have these environment variables in all operating systems, Bash is used to mange them in Linux and macOS; and PowerShell is used to mange them in Windows.

We won’t dive too much into, but we will cover the basics of Bash environment variables. To get our current environment variable in Bash, all we have to do is type:

env

Example output:

$ env

SHELL=/bin/bash
TMUX=/tmp/tmux-1000/default,39373,0
PWD=/home/potato/jincx
LOGNAME=potato
XDG_SESSION_TYPE=tty
MOTD_SHOWN=pam
HOME=/home/potato
LANG=C.UTF-8

Notice in the above output, the all capitalized names are the variables, and its value is separated by the “=” equal sign. So if we ever wanted to call a variable to know its value in our current shell; we to prepend the $ and call the variable name:

# $SHELL shows the current shell we are running as
$ echo $SHELL
/bin/bash

# $USER shows the current user we are running as
$ echo $USER
potato

# $PATH shows the relative paths from which we run executables
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/usr/local/go/bin

Absolute and Relative Path

An absolute path is the full path name to a certain file, binary, or folder. We will use the ping binary with its absolute path as an example:

/usr/bin/ping -h

As for relative paths, it typically defined in our $PATH environment. The directories in our $PATH environment are where Bash checks and runs the commands we use without using the absolute path. We will again use the ping program for our example:

$ ping -h

# since ping binary is in a directory in the $PATH environment
# we call call it directly
$ echo $PATH
... /usr/bin ...

Bash Special Characters

There are a few special characters in Bash with predefined functions, we will look at a few important ones for every Linux user to know.

Tilde Symbol

The tilde ~ symbol is used to refer to the current users’ home directory:

$ echo ~
/home/potato

$ sudo su
[sudo] password for potato:

$ echo ~

/root

Notice above, when we switch to root user, the home folder changes, too.

Star Symbol

Also known as the “wildcard” in computer science. The * symbol is predefined as all. When we use it, we mean to get all of the output in reference to how its called:

$ ls /etc/*.conf
/etc/adduser.conf          /etc/deluser.conf

Above will output everything in the /etc/ directory with the extension .conf

Backticks

This will execute further commands within the backticks and will aide us with Bash scripting:

file `ls -la`

# alternative method
file $(ls -la)

Bash Output Redirectors

To redirect output into a file, we can use the less than or greater than symbols < >:

# overwrite a file with output
echo "This will overwrite this sentence into the file" > newFile.txt

# to append to a file
echo "This sentence will be on the last line and will not overwrite previous" >> oldFile.txt

Bash Command Chaining

We can also chain different commands together to narrow down the output we are looking for. We do this by using the pipe | symbol and the &&:

ls /etc/*.conf | less

Another example:

cat filename.txt && echo "Append this to the file" >> filname.txt | less 

Conclusion

These are the most basic commands to learn in order to use Linux and remember; “Practice makes perfect”. That will be all for this post, but stay tuned! We will soon post further into scripting with Bash. Also, we will post basic quick guides on Windows command prompt and PowerShell.