Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Shell Scriptingfor Beginners Howto Write Bash Scriptsin Linux

This document provides an introduction to shell scripting in Linux using Bash. It explains that Bash scripts allow you to automate common tasks by writing sequences of commands in a file. The document covers the basics of Bash scripts, including how to identify them, create a simple "Hello World" script, and add execution permissions. It also discusses using cron jobs to schedule automated scripts.

Uploaded by

Kyla Pineda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Shell Scriptingfor Beginners Howto Write Bash Scriptsin Linux

This document provides an introduction to shell scripting in Linux using Bash. It explains that Bash scripts allow you to automate common tasks by writing sequences of commands in a file. The document covers the basics of Bash scripts, including how to identify them, create a simple "Hello World" script, and add execution permissions. It also discusses using cron jobs to schedule automated scripts.

Uploaded by

Kyla Pineda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux

Forum Donate

Learn to code — free 3,000-hour curriculum

MARCH 31, 2022 / #BASH

Shell Scripting for Beginners –


How to Write Bash Scripts in
Linux
Zaira Hira

Shell scripting is an important part of process


automation in Linux. Scripting helps you write a
sequence of commands in a file and then execute them.

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.

In this article, you will learn:

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

1. What is a bash shell? Forum Donate

2. What is a bash script


Learn and—how
to code freedo you identify
3,000-hour it?
curriculum

3. How to create your first bash script and execute it.

4. The basic syntax of shell scripting.

5. How to see a system's scheduled scripts.

6. How to automate scripts by scheduling via cron jobs.

The best way to learn is by practicing. I highly encourage you to follow


along using Replit. You can access a running Linux shell within
minutes.

Introduction to the Bash Shell


The Linux command line is provided by a program called the shell.
Over the years, the shell program has evolved to cater to various
options.

Different users can be configured to use different shells. But most


users prefer to stick with the current default shell. The default shell
for many Linux distros is the GNU Bourne-Again Shell (bash). Bash is
succeeded by Bourne shell ( sh ).

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.

When a shell is used interactively, it displays a $ when it is waiting for


a command from the user. This is called the shell prompt.

[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

If shell is running as root, the prompt is changed to # . The superuser Donate


Forum
shell prompt looks like this:
Learn to code — free 3,000-hour curriculum

[root@host ~]#

Bash is very powerful as it can simplify certain operations that are


hard to accomplish efficiently with a GUI. Remember that most
servers do not have a GUI, and it is best to learn to use the powers of a
command line interface (CLI).

What is a Bash Script?


A bash script is a series of commands written in a file. These are read
and executed by the bash program. The program executes line by line.

For example, you can navigate to a certain path, create a folder and
spawn a process inside it using the command line.

You can do the same sequence of steps by saving the commands in a


bash script and running it. You can run the script any number of times.

How Do You Identify a Bash


Script?
File extension of .sh .
By naming conventions, bash scripts end with a .sh . However, bash
scripts can run perfectly fine without the sh extension.

Scripts start with a bash bang.

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

Scripts are also identified with a shebang . Shebang is a combination


Forum of
Donate
bash # and bang !  followed the the bash shell path. This is the first
Learn to code — free 3,000-hour curriculum
line of the script. Shebang tells the shell to execute it via bash shell.
Shebang is simply an absolute path to the bash interpreter.

Below is an example of the shebang statement.

#! /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.

An execution right is represented by x . In the example below, my user


has the rwx (read, write, execute) rights for the file test_script.sh

File colour
Executable scripts appear in a different colour from rest of the files
and folders.

In my case, the scripts with execution rights appear as green.

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

Learn to code — free 3,000-hour curriculum

How to Create Your First Bash


Script
Let's create a simple script in bash that outputs Hello World .

Create a file named hello_world.sh

touch hello_world.sh

Find the path to your bash shell.

which bash

In my case, the path is /usr/bin/bash and I will include this in the


shebang.

Write the command.


https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 5/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux

We will echo "hello world" to the console. Forum Donate

Learn to code — free 3,000-hour curriculum


Our script will look something like this:

#! /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.

Provide execution rights to your user.


Modify the file permissions and allow execution of the script by using
the command below:

chmod u+x hello_world.sh

chmod modifies the existing rights of a file for a particular user. We


are adding +x to user u .

Run the script.


You can run the script in the following ways:

./hello_world.sh

bash hello_world.sh .

Here's the output:


https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 6/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux

Forum Donate

Learn to code — free 3,000-hour curriculum

Two ways to run scripts

The Basic Syntax of Bash


Scripting
Just like any other programming language, bash scripting follows a set
of rules to create programs understandable by the computer. In this
section, we will study the syntax of bash scripting.

How to define variables


We can define a variable by using the syntax variable_name=value . To
get the value of the variable, add $ before the variable.

#!/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

Learn to code — free 3,000-hour curriculum

Tux is also the name of the Linux mascot, the penguin.

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

Let's run a few examples. Forum Donate

Learn to code — free 3,000-hour curriculum

Note the spaces, these are part of the syntax

Numerical expressions can also be calculated and stored in a variable


using the syntax below:

var=$((expression))

Let's try an example.

#!/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

Fractions are not correctly calculated using the above methods


Forum and Donate
truncated.
Learn to code — free 3,000-hour curriculum

For decimal calculations, we can use bc command to get the output


to a particular number of decimal places. bc (Bash Calculator) is a
command line calculator that supports calculation up to a certain
number of decimal points.

echo "scale=2;22/7" | bc

Where scale defines the number of decimal places required in the


output.

Getting output to 2 decimal places

How to read user input


Sometimes you'll need to gather user input and perform relevant
operations.

In bash, we can take user input using the read command.

read variable_name

To prompt the user with a custom message, use the -p flag.

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

read -p "Enter your age" variable_name Forum Donate

Learn to code — free 3,000-hour curriculum


Example:

#!/bin/bash

echo "Enter a numner"


read a

echo "Enter a numner"


read b

var=$((a+b))
echo $var

Numeric Comparison logical operators


Comparison is used to check if statements evaluate to true or
false . We can use the below shown operators to compare two
statements:

OPERATION SYNTAX EXPLANATION

Equality num1 -eq num2 is num1 equal to num2


https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 11/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux

OPERATION SYNTAX EXPLANATION Forum Donate

Learn to code — free 3,000-hour curriculum


Greater than equal to num1 -ge num2 is num1 greater than equal to num2

Greater than num1 -gt num2 is num1 greater than num2

Less than equal to num1 -le num2 is num1 less than equal to num2

Less than num1 -lt num2 is num1 less than num2

Not Equal to num1 -ne num2 is num1 not equal to num2

Syntax:

if [ conditions ]
then
commands
fi

Example:

Let's compare two numbers and find their relationship:

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

Learn to code — free 3,000-hour curriculum

Output:

Conditional Statements (Decision Making)


Conditions are expressions that evaluate to a boolean expression
( true or false ). To check conditions, we can use if , if-else , if-
elif-else and nested conditionals.

The structure of conditional statements is as follows:

if...then...fi statements

if...then...else...fi statements

if..elif..else..fi

if..then..else..if..then..fi..fi.. (Nested Conditionals)

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

To create meaningful comparisons, we can use AND -a and OR -o as


well.

The below statement translates to: If a is greater than 40 and b is


less than 6.

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

Output: Forum Donate

Learn to code — free 3,000-hour curriculum


Test case #1

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

Learn to code — free 3,000-hour curriculum

Test case #3

Looping and skipping


https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 16/27
5/22/23, 9:13 AM Shell Scripting for Beginners – How to Write Bash Scripts in Linux

For loops allow you to execute statements a specific number


Forumof times.Donate

Learn to code — free 3,000-hour curriculum


Looping with numbers:
In the example below, the loop will iterate 5 times.

#!/bin/bash

for i in {1..5}
do
echo $i
done

Looping with strings:


We can loop through strings as well.

#!/bin/bash

for X in cyan magenta yellow


do

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

Learn to code — free 3,000-hour curriculum

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.

In the example below, (( i += 1 )) is the counter statement that


increments the value of i .

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

Learn to code — free 3,000-hour curriculum

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

while read -r CURRENT_LINE


Forum Donate
do
Learn to code — free 3,000-hour curriculum
echo "$LINE: $CURRENT_LINE"
((LINE++))
done < "sample_file.txt"

Output:

Lines with line number printed

How to execute commands with back ticks


If you need to include the output of a complex command in your script,
you can write the statement inside back ticks.

Syntax:
var= ` commands `

Example: Suppose we want to get the output of a list of mountpoints


with tmpfs in their name. We can craft a statement like this: df -h |
grep tmpfs .

To include it in the bash script, we can enclose it in back ticks.

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

Learn to code — free 3,000-hour curriculum


var=`df -h | grep tmpfs`
echo $var

Output:

How to get arguments for scripts from the


command line
It is possible to give arguments to the script on execution.

$@ represents the position of the parameters, starting from one.

#!/bin/bash

for x in $@
do
echo "Entered arg is $x"
done

Run it like this:

./script arg1 arg2

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

Learn to code — free 3,000-hour curriculum

How to Automate Scripts by


Scheduling via cron Jobs
Cron is a job scheduling utility present in Unix like systems. You can
schedule jobs to execute daily, weekly, monthly or in a specific time of
the day. Automation in Linux heavily relies on cron jobs.

Below is the syntax to schedule crons:

# Cron job example


* * * * * sh /path/to/script.sh

Here, * represents minute(s) hour(s) day(s) month(s) weekday(s),


respectively.

Below are some examples of scheduling cron jobs.

SCHEDULE SCHEDULED VALUE

50*8* At 00:05 in August.

54**6 At 04:05 on Saturday.

0 22 * * 1-5 At 22:00 on every day-of-week from Monday through Friday.

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

Learn to code — free 3,000-hour curriculum

How to Check Existing Scripts in a


System
Using crontab
crontab -l lists the already scheduled scripts for a particular user.

My scheduled scripts

Using the find command


The find command helps to locate files based on certain patterns. As
most of the scripts end with .sh , we can use the find script like this:

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

find . -type f -name "*.sh"


Forum Donate

Learn to code — free 3,000-hour curriculum


`

Where,

. represents the current directory. You can change the path


accordingly.

-type f indicates that the file type we are looking for is a text
based file.

*.sh tells to match all files ending with .sh .

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

You can read my other posts here. Forum Donate

Learn to code — free 3,000-hour curriculum


Work vector created by macrovector - www.freepik.com

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

Learn to code for free. freeCodeCamp's open source curriculum has


helped more than 40,000 people get jobs as developers.
Get started
ADVERTISEMENT

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States


Federal Tax Identification Number: 82-0779546)

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.

You can make a tax-deductible donation here.

Trending Guides

What is a Framework? SQL HAVING

What Do CS Majors Do? What is OOP?

Discord Update Failed HTML textarea

Center an Image in CSS NVM for Windows

What is the MVC Model? Git Revert File

JavaScript replaceAll() GROUP BY in SQL

Python Switch Statement 2D Array in Java

Python string.replace() How to Install NVM

What is a Relational DB? Percentages in Excel

Split a String in Python JavaScript Timestamp

Git List Remote Branches Remove Item from Array JS

Git Delete Remote Branch Dual Boot Windows + Ubuntu

Software Developer Career Python Round to 2 Decimals

Three Dots Operator in JS String to Int in JavaScript

How to Format Dates in JS What’s the .gitignore File?


Our Charity

About Alumni Network Open Source Shop Support Sponsors Academic Honesty

Code of Conduct Privacy Policy Terms of Service Copyright Policy

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

Learn to code — free 3,000-hour curriculum

https://www.freecodecamp.org/news/shell-scripting-crash-course-how-to-write-bash-scripts-in-linux/ 27/27

You might also like