>$ ./shell.sh
Here is a list of commands that are commonly used in running bash scripts. The commands are ordered from Basic to Advanced.
which $BASH
output: /bin/bash
Use echo to print to terminal (output)
echo "Hi there!."
Use #!/bin/bash
at the top of every bash script to save a bash executable
nano
is a poweful ide for bash
sleep 3
to sleep 3 seconds.
nano ls -l
# - to see permissions e.g rw-r--r-- for read and write
chmod +x hithere.sh
# - to add execute permission to files
./hithere.sh
# - to run the file with execute permissions
In bash, variables are used to store values. There are two types of variables:
Scalar variables: Store a single value. Array variables: Store multiple values. Declaring Variables
To declare a variable, use the = operator:
MY_VAR="Hello, World!"
Using Variables
To use a variable, prefix it with the $ symbol:
# use a variable
echo $MY_VAR
variableName="Mr Cool Variable"
echo "Good morning $name!"
# Output: Good morning Mr Cool Variable!"
Use read to access user typed input.
echo what is your name?
read name
Let us assume the user types >_ Username
on the terminal
echo "What'up $name!"
Output:
What'up Username!
These are used to access parameters passed when calling the file
./call-file.sh Malaika Mrembo
This command will run a file called callme.sh
and pass the parameters Malaika
and Mrembo
.
# filename callme.sh
name=$1
complement=$2
echo "Jambo $name!! Wewe ni $complement."
output:
Jambo Malaika!! Wewe ni Mrembo.
# who am I (current logged in user)?
whoami # > output - root
# print current working directory (where am I?)
pwd
# print current date (dateTime format timestamp UTC)
date # output: Thu Jan 31 01:53:04 PM UTC 2024
How to use commandline output as a variable or parameter.
# Saving the name of the currently logged in user in a variable
user=$(whoami)
$date =$(date)
echo "You are currently logged in as $user. The time is $date."
Bash has built-in variables.
echo $USER # > output: root (currently logged in user)
echo $SHELL # > output: /bin/bash
echo $PWD # > output: /root (print current working directory)
echo $HOSTNAME # > output: localhost
Create variables and use them in child files using export
.
$twitter="Elon Musk's X!"
export twitter
# these variables will be lost on exit
Make the variables permanent.
ls -al provides details of files in a directory with permissions and users details
.bashrc
is a script that runs on each startup.
# file name: .bashrc
# add the following at the end of the file
export twitter="Elon Musk X!"
Next, call the variable echo $twitter
and the output will be Elon Musk
. So, the output has been persisted over two sections. Now, it is permanent.
$RANDOM
is a built-in variable that returns a random number upto 32767.
echo $RANDOM
Bash provides various operators for performing arithmetic, comparison, and logical operations.
+
(addition)
-
(subtraction)
*
(multiplication)
/
(division)
%
(modulus)
==
(equal to)
!=
(not equal to)
<
(less than)
>
(greater than)
<=
(less than or equal to)
>=
(greater than or equal to)
&&
(and)
||
(or)
!
(not)
Control structures are used to control the flow of a script.
if [ condition ]; then
# code to execute if condition is true
else
# code to execute if condition is false
fi
for var in values; do
# code to execute for each value
done
while [ condition ]; do
# code to execute while condition is true
done
Functions are reusable blocks of code that can be called multiple times in a script.
my_function() {
# code to execute
}
my_function
Bash provides various ways to interact with the user and display output.
read -p "Enter your name: " NAME
echo "Hello, $NAME!"
Bash provides various commands for manipulating files and directories.
touch myfile.txt
rm myfile.txt
mv myfile.txt mydir/
cp myfile.txt mydir/
Conditional statements are used to execute code based on certain conditions.
if [ -f myfile.txt ]; then
echo "File exists!"
fi
case $MY_VAR in
"value1")
# code to execute for value1
;;
"value2")
# code to execute for value2
;;
*)
# code to execute for default
;;
esac
Bash supports regular expressions for pattern matching.
if [[ $MY_VAR =~ ^[a-zA-Z]+$ ]]; then
echo "Variable contains only letters!"
fi
1. Parameter Expansion
Parameter expansion allows you to manipulate variables using various operators.
Example
MY_VAR="hello world"
echo ${MY_VAR^^} # outputs "HELLO WORLD"
2. Command Substitution
Command substitution allows you to execute a command and store its output in a variable.
Example
MY_VAR=$(ls -l)
echo $MY_VAR
3. Process Substitution Process substitution allows you to treat the output of a command as a file.
Example
diff <(ls -l) <(ls -l /mydir)
4. Arrays Arrays are used to store multiple values in a single variable.
Example
MY_ARRAY=("value1" "value2" "value3")
echo ${MY_ARRAY[0]} # outputs "value1"
5. Bashisms Bashisms are features specific to Bash that can make your scripts more efficient.
Example
MY_VAR="hello world"
echo ${MY_VAR:0:5} # outputs "hello"
cd
Change directory.
mkdir
Create a new directory.
rm
Delete a file or directory.
cp
Copy a file or directory.
mv
Move or rename a file or directory.
echo
Display output to the console.
grep
Search for patterns in files.
find
Search for files based on various criteria.
sed
Stream editor for manipulating text.
awk
Pattern scanning and processing language.
Basic Math!
echo $((2 + 3))
echo $(( 3/4)) # output: 0
echo $(( 5 % 4)) # output: 1
echo "What is your age?"
read $name
echo $(( $name % 5)) # output: 2