1. Home
  2. Linux
  3. Bash scripts to automate linux

How To Write Bash Scripts To Automate Linux

Bash scripting is a useful skill to learn as a Linux user. Even as a beginner, knowing this skill can be the difference between taking full advantage of your machine, and doing everything by hand. That’s why in this article, we’ll teach you how to write bash scripts to automate Linux, and take full control of your desktop. We’ll go over the basics of scripting with the Bash shell, the basics of what makes a script, how to run them on your system, what shebangs mean, and more!

Types Of Scripts

There are many different types of scripts. Mainly: SH and BASH. These file extensions are important, because the file extension tells the interpreter how to run it. If a file is a SH file, it can be run in any shell, not just Bash. This means a script written for Bash on Linux can run on the Mac, BSD, and other places with similar shells.

Scripts using the BASH file extension are only meant to run inside Bash. Suffice it to say, your script with the bash file extension won’t run in the Fish shell on Linux, or any of the other similar shells available.

Shebangs

Some scripts don’t use file extensions at all. Instead, they use a shebang to let the interpreter understand what the script is for, and how to run it. When writing a script, the shebang ALWAYS comes first. If a shebang is absent, often times a script will refuse to run, especially if no file extension is being used.

There’s a lot to learn about shebangs, but beginners need not worry about them. They only prove useful for advanced Bash users, looking to write incredibly complex Bash tools. As beginners, the only shebang that users need to think about is the standard one: #!/bin/bash

Making A Bash script

Many beginners confuse bash scripts for actual programming. The main purpose of scripting in bash is to string many commands together, effectively doing long complicated things without the need to write every little thing out in the terminal shell. If you’re trying to do serious programming in Bash, stop and consider an actual programming language utilized on Linux like Python.

To start off, open a terminal and enter the following command:

nano myfirstbashscript

This brings up the Nano text editor. Inside nano, add the shebang, to help the interpreter identify what to do with the script.

#!/bin/bash

From here, we can do anything! For example: an Ubuntu user can make a simple update script by adding the lines:

sudo apt update;sudo apt upgrade -y

Alternatively, another example: make a continuous ping script to run in the background, if you’re concerned about your network speed.

ping google.com

Add any command(s) you’d like to the script! Get creative!

After adding the what you want to your script, save it with nano using CTRL + O.

With the script created, it’s time to update the permissions. In a terminal, do:

sudo chmod +x myfirstbashscript

Running Scripts

To run a shell script, open a terminal and do:

sudo sh script.sh

To run bash files, try:

sudo bash script.bash

Alternatively, any script regardless of the file extension can easily run with ./filename in the terminal.

All three ways to run scripts work very well. That being said, using ./filename is the worst way, as scripts often won’t run unless the bash script has the correct permissions. Set the permissions with:

sudo chmod +x script

Making Your Script A Binary

To run your script by just typing the name of the file in the terminal, you’ll need to make it a binary. To do this, use the chmod command to make it executable.

sudo chmod +x

When the script is marked as an executable, it’s time to move the script so that it’s accessible in the user’s path. Do this with the MV command. Alternatively, use the CP command instead of the move command if you want to keep a backup of your script in it’s original location.

sudo mv /location/of/script /usr/bin/

or

sudo cp /location/of/script /usr/bin/

Then, run the newly moved script inside the terminal, from anywhere simply by typing the name of it in the terminal. For example: you used YouTube-DL to write an automatic YouTube ripping script, and placed it in /usr/bin/ for easy access.

To run said script, you’d do:

youtubescript

That’s it!

Conclusion

Learning the language of bash is a useful tool. Without it, system administrators and terminal geeks would find themselves slaving over a terminal for hours at a time. Instead, they can write bash scripts to automate Linux. By learning to harness the power of Bash, you write all these complex operations out, and run them quickly with a script.

The shell is beautiful, and the more you learn about it, the more you’ll learn about their own Linux systems, how to automate them, and even improve how things work internally! The sky’s the limit with bash, all it takes is a little creativity!

1 Comment

  1. “Many beginners confuse bash scripts for actual programming. ” Writing a bash script (aka program) is just as much of a programming task as writing a Python program. Bash is a high level language where many of the imperatives happen to be other scripts or compiled programs. By the way, Python programs are scripts also. The traditional differentiation between script and program is whether it is interpreted or compiled to machine language. It that difference important? Not really. Write the thing however you need to get the job done using the right tool (or at least an adequate tool) for the job.

    Setting the execute permission on a script does make it a binary file, it only makes it executable. The content of the file does not change. Just because a file is binary does not make it executable, for example, an image file. At the lowest level everything is in binary but binary is rather inconvenient for people to work with.