Bash Scripting Basics

To assign the phrase "Hello World" to the greeting variable, we can use either single quotes (') or double quotes (") to enclose our text. However, Bash treats single and double quotes differently. When encountering single quotes, Bash interprets every enclosed character literally. When enclosed in double quotes, all characters are viewed literally except "$", "`", and "" meaning variables will be expanded in an initial substitution pass on the enclosed text.

$ greeting='Hello World'
$ echo $greeting
Hello World
$ greeting2="New $greeting"
$ echo $greeting2
New Hello World

We can also set the value of the variable to the result of a command or program. This is known as command substitution, which allows us to take the output of a command or program (what would normally be printed to the screen) and have it saved as the value of a variable.

To do this, place the variable name in parentheses "()", preceded by a "$" character:

$ user=$(whoami)
$ echo $user
kali

For additional debug output add -x to the shebang: #!/bin/bash -x

Command-line Arguments

Variable Name Description
$0 The name of the Bash script
$1 - $9 The first 9 arguments to the Bash script
$# Number of arguments passed to the Bash script
$@ All arguments passed to the Bash script
$? The exit status of the most recently run process
$$ The process ID of the current script
$USER The username of the user running the script
$HOSTNAME The hostname of the machine
$RANDOM A random number
$LINENO The current line number in the script

User Input

We can alter the behavior of the read command with various command line options. Two of the most commonly used options include -p, which allows us to specify a prompt, and -s, which makes the user input silent. The latter is ideal for capturing user credentials.

$ cat ./input2.sh
#!/bin/bash
# Prompt the user for credentials

read -p 'Username: ' username
read -sp 'Password: ' password

echo "Thanks, your creds are as follows: " $username " and " $password

$ chmod +x ./input2.sh
$ ./input2.sh
Username: kali
Password: 
Thanks, your creds are as follows:  kali  and  nothing2see!

Conditional Statements

$ cat ./if.sh
#!/bin/bash
# if statement example

read -p "What is your age: " age

if [ $age -lt 16 ]
then
  echo "You might need parental permission to take this course!"
fi

$ chmod +x ./if.sh 

$ ./if.sh 
What is your age: 15
You might need parental permission to take this course!

In this example, we used an if statement to check the age entered by a user. If the entered age was less than (-lt) 16, the script would output a warning message.

The square brackets ("[" and "]") in the if statement above are actually a reference to the test command. This simply means we can use all of the operators that are allowed by the test command. Some of the most common operators include the following.

Operator Description: Expression True if...
!EXPRESSION The EXPRESSION is false.
-n STRING STRING length is greater than zero
-z STRING The length of STRING is zero (empty)
STRING1 != STRING2 STRING1 is not equal to STRING2
STRING1 = STRING2 STRING1 is equal to STRING2
INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2
INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2
INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER 2
INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER 2
-d FILE FILE exists and is a directory
-e FILE FILE exists
-r FILE FILE exists and has read permission
-s FILE FILE exists and it is not empty
-w FILE FILE exists and has write permission
-x FILE FILE exists and has execute permission
if [ <some test> ]
then
  <perform action>
elif [ <some test> ]
then
  <perform different action>
else
  <perform yet another different action>
fi

Boolean Logical Operations

Loops

For Loops

for var-name in <list>
do
  <action to perform>
done

While Loops

counter = 0
while [ <some test> ]
do
  <perform an action>
  ((counter++))
done

Relevant Note(s): Linux Basics