Sunday, December 29, 2024
Google search engine
HomeGuest BlogsHow to Write a Bash Script with Examples

How to Write a Bash Script with Examples

Introduction

Bash is a Unix command line interface for interacting with the operating system, available for Linux and macOS. Bash scripts help group commands to create a program. All instructions that run from the terminal work in Bash scripts as well.

Bash scripting is a crucial tool for system administrators and developers. Scripting helps automate repetitive tasks and interact with the OS through custom instruction combinations. The skill is simple to learn and requires only basic terminal commands to get started.

This guide will show how to write a Bash script and Bash scripting basics through examples.

How to write a bash script with examplesHow to write a bash script with examples

Prerequisites

Writing a Bash Script

To start with Bash scripting, create a new file using a text editor. If you’re using Vim, run the following command:

vim script.sh

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

Adding the “shebang”

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

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

#!/bin/bash

The shebang consists of the following elements:

  • #! directs the program loader to load an interpreter for the code in the file.
  • /bin/bash the Bash interpreter’s location.

Some typical shebang lines for different interpreters are in the table below.

Shebang Interpreter
#!/bin/bash Bash
#!/bin/sh Bourne shell
#!/usr/bin/env <interpreter> Uses the env program to locate the interpreter. Use this shebang for other scripting languages, such as Perl, Python, etc.
#!/usr/bin/pwsh Powershell

After adding a shebang, continue to the next section.

Note: Learn how to evaluate arithmetic expressions using Bash let.

Adding Comments

Comments are lines that do not execute. 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
script.sh commentscript.sh comment

For more information on Bash comments and the best practices, read our article on how to comment in Bash.

Adding Code

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

#!/bin/bash
# A simple Bash script
sudo apt update -y
sudo apt upgrade -y
echo Done!
script.sh code commandsscript.sh code commands

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

Save the code and exit the text editor.

Executing the Bash Script

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

bash script.sh
bash script.sh update script terminal outputbash script.sh update script terminal output

The script prompts to enter the password to run the sudo commands. Enter the password and wait for the program to finish the update and upgrade.

Note: Learn how to use the Bash read command.

Conclusion

By following the steps in this tutorial, you should have a simple script to update and upgrade the system. Customize the script further or create a new script that does something different. Our guide could help you to start creating custom bash functions which could help you write more efficient bash scripts.

To further deepen your Bash knowledge, learn how to write a script to check if a file or directory exists in Bash.

Read our article Bash math operations (Bash arithmetic) and learn about different bash math commands and methods, and why is math important in Bash scripting.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments