# How To Write Bash Scripts on Linux

**URL:** https://www.compsmag.com/blogs/how-to-write-bash-scripts-on-linux/
**Author:** Ayushi Chauhan
**Published:** 2022-04-18
**Updated:** 2022-04-18
**Categories:** Blogs
**Tags:** Blogs Daily, Computer Guides, guiderobert, Guides and Tutorials, guiding12, How To Guide, How To Guides, Linux Guides, Tips, Tricks
**Reading Time:** 3 min

---

This tip is about the how To Write Bash Scripts on Linux. So read this free guide, How To Write Bash Scripts on Linux step by step. If you have query related to same article you may contact us.

## How To Write Bash Scripts on Linux - Guide

Shell scripts are an important part of process automation on Linux.  Scripts allow you to write a series of commands to a file and then run them.  This saves you time because you don't have to rewrite certain commands over and over again.  You can efficiently perform daily tasks and even schedule them to run automatically.  You can also specify that certain scripts run on startup, such as displaying a certain message when a new session is started, or setting certain environment variables.

A bash script is a series of commands that are written to a file.  These are read and executed by the bash program.  The program runs line by line.  For example, you can navigate to a specific path, create a folder, and start a process in that folder from the command line.  You can perform the same sequence of steps by storing the commands in a Bash script and running it.  You can run the script as many times as you like.

## How to Write Bash Scripts on Linux

### Writing a Bash Script

To get started with the Bash script, create a new file using a text editor.  If you are using Vim, run the following command:

The extension for Bash scripts is .sh.  However, the extension is not required.  Adding the .sh makes the file easy to identify and maintain.

### Adding the “shebang”

The first line in Bash scripts is a string known as a “shebang”.  The shebang is the program loader's first instruction when executing the file, and the characters indicate which interpreter to execute when reading the script.

Add the following line to the file to indicate the use of the Bash interpreter:

The shebang consists of the following elements:

#!  directs the program loader to load an interpreter for the code in the file.

/bin/bash the location of the Bash interpreter.

### adding comments

Comments are lines that are not executed.  However, they help with code readability.  After the shebang, add a comment to explain what the script is.

For example:

#!/bin/bash

# A simple Bash script

### adding code

As an example, create a script to update and update the system.  Add the lines after the Bash comment so that the final script looks like the following:

#!/bin/bash

# A simple Bash script

sudo apt update -y

sudo apt upgrade -y

echo Done!

The Bash interpreter reads each line and executes the update command followed by the update command.  The -y tag automatically responds Yes to any prompt brought up up by the two instructions.  When completed, the program prints Done!  to the console.

### Running the Bash Script

To run the Bash script, use the following command in the terminal:

## Final note

I hope you like the guide How To Write Bash Scripts on Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.

---

*End of Article*