Shell Scriptingfor Beginners Howto Write Bash Scriptsin Linux
Shell Scriptingfor Beginners Howto Write Bash Scriptsin Linux
Forum Donate
This saves you time because you don't have to write certain
commands again and again. You can perform daily tasks efficiently and
even schedule them for automatic execution.
You can also set certain scripts to execute on startup such as showing
a particular message on launching a new session or setting certain
environment variables.
The applications and uses of scripting are numerous, so let's dive in.
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 1/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
When you first launch the shell, it uses a startup script located in the
.bashrc or .bash_profile file which allows you to customize the
behavior of the shell.
[username@host ~]$
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 2/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
[root@host ~]#
For example, you can navigate to a certain path, create a folder and
spawn a process inside it using the command line.
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 3/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
#! /bin/bash
The path of the bash program can vary. We will see later how to
identify it.
Execution rights
Scripts have execution rights for the user executing them.
File colour
Executable scripts appear in a different colour from rest of the files
and folders.
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 4/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
Forum Donate
touch hello_world.sh
which bash
#! /usr/bin/bash
echo "Hello World"
Edit the file hello_world.sh using a text editor of your choice and add
the above lines in it.
./hello_world.sh
bash hello_world.sh .
Forum Donate
#!/bin/bash
# A simple variable example
greeting=Hello
name=Tux
echo $greeting $name
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 7/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
Forum Donate
Hi, I am Tux.
Arithmetic Expressions
Below are the operators supported by bash for mathematical
calculations:
OPERATOR USAGE
+ addition
- subtraction
* multiplication
/ division
** exponentiation
% modulus
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 8/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
var=$((expression))
#!/bin/bash
var=$((3+9))
echo $var
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 9/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
echo "scale=2;22/7" | bc
read variable_name
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 10/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
#!/bin/bash
var=$((a+b))
echo $var
Less than equal to num1 -le num2 is num1 less than equal to num2
Syntax:
if [ conditions ]
then
commands
fi
Example:
read x
read y
if [ $x -gt $y ]
then
echo X is greater than Y
elif [ $x -lt $y ]
then
echo X is less than Y
elif [ $x -eq $y ]
then
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 12/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
echo X is equal to Y
fi
Forum Donate
Output:
if...then...fi statements
if...then...else...fi statements
if..elif..else..fi
Syntax:
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 13/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
Forum Donate
if [[ condition ]]
Learn to code — free 3,000-hour curriculum
then
statement
elif [[ condition ]]; then
statement
else
do this by default
fi
if [ $a -gt 40 -a $b -lt 6 ]
Example: Let's find the triangle type by reading the lengths of its sides.
read a
read b
read c
if [ $a == $b -a $b == $c -a $a == $c ]
then
echo EQUILATERAL
elif [ $a == $b -o $b == $c -o $a == $c ]
then
echo ISOSCELES
else
echo SCALENE
fi
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 14/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
Test case #2
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 15/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
Forum Donate
Test case #3
#!/bin/bash
for i in {1..5}
do
echo $i
done
#!/bin/bash
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 17/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
echo $X
done
Forum Donate
While loop
While loops check for a condition and loop until the condition remains
true . We need to provide a counter statement that increments the
counter to control loop execution.
Example:
#!/bin/bash
i=1
while [[ $i -le 10 ]] ; do
echo "$i"
(( i += 1 ))
done
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 18/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
Forum Donate
Reading files
Suppose we have a file sample_file.txt as shown below:
We can read the file line by line and print the output on the screen.
#!/bin/bash
LINE=1
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 19/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
Output:
Syntax:
var= ` commands `
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 20/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
#!/bin/bash
Forum Donate
Output:
#!/bin/bash
for x in $@
do
echo "Entered arg is $x"
done
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 21/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
Forum Donate
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 22/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
You can learn about cron in detail in this blog post. Forum Donate
My scheduled scripts
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 23/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
Where,
-type f indicates that the file type we are looking for is a text
based file.
If you are interested to read about the find command in detail, check
my other post.
Wrapping up
In this tutorial we learned the basics of shell scripting. We looked into
examples and syntax which can help us write meaningful programs.
What’s your favorite thing you learned from this tutorial? Let me
know on Twitter!
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 24/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
Zaira Hira
I am a DevOps Consultant and writer at FreeCodeCamp. I aim to provide easy
and to-the-point content for Techies!
If you read this far, tweet to the author to show them you care.
Tweet a thanks
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 25/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
Our mission: to help people learn to code for free. We accomplish this by creating
Forum thousands of
Donate
videos, articles, and interactive coding lessons - all freely available to the public. We also have
Learn to code — free 3,000-hour curriculum
thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers,
services, and staff.
Trending Guides
About Alumni Network Open Source Shop Support Sponsors Academic Honesty
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 26/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux
Forum Donate
https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 27/27