Intro to bash scripting and command line
My study resources are the Bash Scripting Tutorial article from freeCodeCamp and Introduction to Bash Guide from Devops Daily
Basic Bash Commands
date: Displays the current date in the terminal
date
Thu Sep 24 12:04:57 EEST 2026
pwd: Displays the present working directory
pwd
/Users/mas/documents
ls: Lists the contents of the current directory
ls
images.jpeg folder file.txt
echo: Prints a string of text, or value of a variable to the terminal
echo "Hello world"
Hello world
cd: Change the directory to a different location.
cd documents
~/documents$
mkdir: Create a new directory.
mkdir newFolder
ls
newFolder GitHub
touch: Create a new file.
touch file.txt
rm: Remove a file or directory.
rm file.txt
cp: Copy a file or directory.
cp file.txt ~/documents/
mv: Move or rename a file or directory.
mv file.txt ~/documents/newfile/
echo: Print text to the terminal.
echo "hello world"
hello world
cat: Concatenate and print the contents of a file.
cat file.txt
hello world,
this is an example
grep: Search for a pattern in a file.
grep "hello" file.txt
chmod: Change the permissions of a file or directory.
chmod u+x run_all.sh
sudo: Run a command with administrative privileges.
sudo [command]
df: Display the amount of disk space available.
df
history: Show a list of previously executed commands.
history
ps: Display information about running processes.
ps
Bash Script File
you can create a Bash script without .sh you can start the file with a shebang (# + ! + path to Bash) like this :
#!/bin/bash
you can find the bash path using which bash command inside your terminal
First File
create run_all.sh file then add this script to it
#!/bin/bash
# print today's date
echo "Today is " `date`
# asks for the path
echo -e "\n Enter the path to the directory"
read the_path
# Lists the contents of the path
echo -e "\n Your path has the following filess and folders: "
ls $the_path
To make the script executable, assign execution rights to your user using this command:
chmod u+x run_all.sh
chmod changes file permissions for the current user, +x adds the execution rights to the current user and u is the current user
then you can run the script using ./run_all.sh
Variables and data types
# assign the value
country=Egypt
# printing the var value
echo $country
Egypt
Variables names are case-sensitive, they must start with a letter or _ and cannot contain spaces or special characters
Input and output
The first input way is read that reads the user input and store it inside a variable
#!/bin/bash
# asking for the name
echo "What's your name?"
# reading the user input
read entered_name
# prints the value with a message
echo -e "\nWelcome to bash tutorial" $entered_name
The second way is reading a file using while loops, the example reads the input.txt file content and prints it in the terminal
while read line
do
echo $line
done < input.txt
yeah i know this is what cat does, practice in silence please
The third way is CLI arguments $1 for the first arg and $2 for the second arg
hello.sh:
#!/bin/bash
echo "Hello, $1!"
when we run it we need to add a first arg to make sure it works
./hello.sh shnawy
Hello, shnawy!
To display the output you use the echo command
# prints hello to terminal
echo "hello"
# prints to the file
echo "This is some text." > output.txt
# append the text on the end of the file
echo "More text." >> output.txt
Conditional statements
I created a simple Bash script that use if and elif and else that asks the user to enter a number between 1 and 100 If they enter a number higher or lower than that it prints a message.
cond.sh
#!/bin/bash
echo "give me a number between 1-100"
read num
if [ $num -gt 100 ]; then
echo "number is greater than 100"
elif [ $num -lt 1 ]; then
echo "numer is lower than 1"
else
echo " Your number is $num "
fi
There’s another shorthand for this using logical operators like -a AND operator , -o OR operator
cond.sh
#!/bin/bash
echo "give me a number betwean 1-100"
read num
if [ $num -gt 100 -o $num -lt 1 ]; then
echo "number out of the range"
else
echo " Your number is $num "
fi
Looping and Branching
While loop
check for a condition and loop until the condition remains true
#!/bin/bash
i=1
while [[ $i -le 10 ]] ; do
echo "$i"
(( i += 1 ))
done
# prints from 1 to 10
For loop
Same idea as a while loop but you iterate over a list
#!/bin/bash
for i in {1..5}
do
echo $i
done
Case statements
a cleaner and more organized alter to long if-else chain, you gave it a value and it compares it against a list of possible options once it gets a match it excute the code for that specific option and skip the other
fruit="apple"
# it checks for the match value for the fruit
# in the list of options to find apple
case $fruit in
"banana")
echo "This is a yellow fruit."
;;
"apple")
echo "This is a red fruit."
;;
"orange")
echo "This is an orange fruit."
;;
*)
echo "Unknown fruit."
;;
esac
"apple") is an option, ;; it’s like break it ends that case, *) is the else version for case statements, esac closes the block
Debug and Troubleshoot Bash Scripts
-
Using
set -xwhen you set it inside the script file the terminal will print each command before executing it -
Using
set -eadding this at the beginning of the script will force the script to stop immediately if any command fail -
Using
set -utells Bash to throw an error and stop the script if it encounters an undeclared variable -
Run
bash -n script.shwill read the file and report any syntax errors -
Manually using
echo