Getting Started with Bash Scripting

Bash scripts are essentially a series of commands written in a file that can be executed by the Bash shell. In this article, we'll cover the basics of creating and running your first Bash script.
Getting Started with Bash Scripting

On this page

Bash scripts are a powerful tool that allow you to automate various tasks on Unix-based systems, such as Linux and macOS. They are essentially a series of commands written in a file that can be executed by the Bash shell. Instead of manually typing and running commands one by one in the terminal, creating a Bash script allows you to save these commands in a file and execute them all at once, making it more efficient and convenient.

In this article, we'll cover the basics of creating and running your first Bash script.

Advantages of bash scripts

There are several advantages to creating Bash scripts:

  1. Automation: Scripts can automate repetitive tasks, saving you time and effort.
  2. Consistency: By running the same set of commands consistently, you can ensure that tasks are performed in the same way every time, reducing the chances of human error.
  3. Portability: Bash scripts can be easily shared and run on different Unix-based systems, making them highly portable.

Create and run your first bash script

To create a new Bash script, open a text editor and create a new file with a .sh extension, for example, my_script.sh. Then, add the following line of code:

#!/bin/bash

echo "Hello, World!"

Save the file and exit the text editor.

Next, open a terminal and navigate to the directory where you saved the script using the cd command. For example, if you saved the script in your home directory, you can use cd ~.

Once you're in the correct directory, you need to make the script executable by running the following command:

chmod +x my_script.sh

This command grants execute permissions to the script file.

Now, you can run the script by typing:

./my_script.sh

You should see the output Hello, World! printed in the terminal.

You can also run your script by typing:

bash my_script.sh

This doesn’t require you to grant execute permissions on the script.

What is Shebang

The first line in your Bash script, #!/bin/bash, is known as the "shebang" or "hashbang." It tells the operating system which interpreter should be used to execute the script. In this case, #!/bin/bash specifies that the Bash shell should be used to run the script.

For example, if you were to run your script using zsh interpreter. Your shebang would look like this:

#!/bin/zsh

Adding your script to PATH

While you can run your script by specifying the full path to the file (/path/to/my_script.sh), it's more convenient to add the script to your system's PATH so you can run it from any directory. Here's how you can do that:

  • Open your shell configuration file (e.g., .bashrc or .bash_profile) in a text editor.
  • Add the following line to the file, replacing /path/to/scripts with the actual path to the directory where you want to store your scripts:
export PATH="$PATH:/path/to/scripts"
  • Save and close the file.
  • Either restart your terminal or run source ~/.bashrc (or source ~/.bash_profile) to apply the changes.

After following these steps, you should be able to run your script by simply typing its name in the terminal, regardless of the current working directory.

Conclusion

In this tutorial, you learned how to create and run your first Bash script, understand the purpose of the shebang line, and add your script to the system's PATH for convenient execution. With these basics under your belt, you can start exploring more advanced Bash scripting concepts and create powerful automation scripts to streamline your workflow.

Subscribe to sysxplore newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox. It's free!
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!