Skip to content
GitHub

Bash Scripting


Bash scripting must have/know tips.

#!/bin/bash
# Hello World Bash Script
echo "Hello World!"
  • Line 1: #! is commonly known as the shebang and is ignored by the Bash interpreter. The second part, /bin/bash, is the absolute path to the interpreter, which is used to run the script. This is what makes this a “Bash script” as opposed to another type of shell script, like a “C Shell script”, for example.
  • Line 2: # is used to add a comment, so all text that follows it is ignored.
  • Line 3: echo “Hello World!” uses the echo Linux command utility to print a given string to the terminal, which in this case is “Hello World!”.

Results in:

./hello-world.sh
Hello World!
Variable NameDescription
$0The name of the Bash script
$1 - $9The 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
$USERThe username of the user running the script
$HOSTNAMEThe hostname of the machine
$RANDOMA random number
$LINENOThe current line number in the scrip
OperatorDescription: Expression True if…
!EXPRESSIONThe EXPRESSION is false.
-n STRINGSTRING length is greater than zero
-z STRINGThe length of STRING is zero (empty)
STRING1 != STRING2STRING1 is not equal to STRING2
STRING1 = STRING2STRING1 is equal to STRING2
INTEGER1 -eq INTEGER2INTEGER1 is equal to INTEGER2
INTEGER1 -ne INTEGER2INTEGER1 is not equal to INTEGER2
INTEGER1 -gt INTEGER2INTEGER1 is greater than INTEGER2
INTEGER1 -lt INTEGER2INTEGER1 is less than INTEGER2
INTEGER1 -ge INTEGER2INTEGER1 is greater than or equal to INTEGER 2
INTEGER1 -le INTEGER2INTEGER1 is less than or equal to INTEGER 2
-d FILEFILE exists and is a directory
-e FILEFILE exists
-r FILEFILE exists and has read permission
-s FILEFILE exists and it is not empty
-w FILEFILE exists and has write permission
-x FILEFILE exists and has execute permission
&&AND
||OR

Prompting user for input and silently reading it using read.

  • -p - prompts the user to supply input, while the input is typed it is shown in the terminal
  • -sp - prompts the user to supply input, while the input is typed it is NOT shown in the terminal
#!/bin/bash
# Prompt the user for credentials
read -p 'Username: ' username
read -sp 'Password: ' password
echo "Thanks, your credentials are as follows: " $username " and " $password 

$ ./input2.sh
Username: crypt0rr
Password:
Thanks, your credentials are as follows: crypt0rr and HaveYouSeenMyPassword?
$ for ip in $(seq 1 5); do echo 10.11.1.$ip; done 
10.11.1.1
10.11.1.2
10.11.1.3
10.11.1.4
10.11.1.5
$ for i in {1..5}; do echo 10.11.1.$i;done 
10.11.1.1
10.11.1.2
10.11.1.3
10.11.1.4
10.11.1.5
#!/bin/bash
# while loop example
counter=1
while [ $counter -lt 6 ]
do
    echo "10.11.1.$counter"
    ((counter++))
done
./while.sh
10.11.1.1
10.11.1.2
10.11.1.3
10.11.1.4
10.11.1.5
#!/bin/bash
# while loop example 2
counter=1
while [ $counter -le 5 ]
do
    echo "10.11.1.$counter"
    ((counter++))
done

./while2.sh 10.11.1.1
10.11.1.2
10.11.1.3
10.11.1.4
10.11.1.5

Two methods of declaring functions.

function function_name {
commands...
}
function_name () {
commands...
}