Skip to content

Streamline mundane tasks using Bash Scripts on Linux systems

Automate your tedious or intricate work tasks using custom scripts for improved efficiency.

Develop custom scripts to handle repetitive or intricate aspects of your professional duties.
Develop custom scripts to handle repetitive or intricate aspects of your professional duties.

Streamline mundane tasks using Bash Scripts on Linux systems

Let's dive into the world of Bash scripts, a powerful tool for automating tasks on Unix and Linux systems. From creating simple scripts to complex system management tools, Bash's versatility shines!

In this tutorial, we'll take our first steps into crafting Bash scripts and explore three example projects. Our journey begins with the classic "Hello World" Bash script, then we'll create interactive scripts, and finally, we'll develop a real script to check the status of a site or server. Later, we'll learn how to make our scripts executable and available system-wide.

Get ready to unleash the power of Bash!

HelloWorld Bash Script

Let's make things simple and start with "Hello World". This may seem boring, but it's a great way to understand the Bash scripting workflow, and to test the basic functionality. We'll use the text editor, but you can also write your scripts in other editors.

  1. Create a new file, , and open it with .
  2. On the first line, specify the interpreter to be used in the code. In this case, it is Bash:
  3. On a new line, use to print a string of text to the screen:
  4. Save thecode by pressing , then press Y and Enter.
  5. Run the code from the terminal.

The output of the command is a single line of "Hello World." Congratulations, you've just created your first Bash script!

Capturing User Input in Bash Scripts

That was fun, but let's make things more interactive. In this example, we'll create a greeting generator that captures user input and chooses a random greeting from an array of greetings.

  1. Create a new file, , with .
  2. Specify the interpreter on the first line:
  3. Add an to print a message to the user:
  4. Read the user's keyboard input, using a prompt to ask a question:
  5. Create an array, , which will store five greetings:
  6. Create a variable to store the number of items in the array:
  7. Create an variable and store a randomly chosen number from zero to the size of the array:
  8. Use to print the randomly chosen greeting along with the user's name:
  9. Save the code by pressing , then press Y and Enter.
  10. Run the code from the terminal, and follow the instructions to see the greeting.

Using Arguments with Bash Scripts

Bash allows you to pass arguments to a command. In this example, we'll write a script to check the status of a URL passed as an argument.

  1. Create a new file, , with .
  2. On the first line, specify the interpreter:
  3. Using , write a message to the user to advise them on the current HTTP response code for the URL:
  4. Create a variable to store the output of the command:
  5. Use to print the HTTP response code to the terminal:
  6. Save the code by pressing , then press Y and Enter.
  7. Run the code from the terminal, make sure to include a full domain name to check.

Creating a system-wide executable Bash Script

Right now, our scripts work well, but they're not available system-wide as executable files. Let's change that. For this final part, we'll convert the script into an application, which we can use from any location on the drive.

First, we need to make the file executable, then we can copy the file to to create an executable available anywhere.

  1. Open a terminal window and navigate to .
  2. Use to set the file as executable:
  3. Run the script, using to instruct the terminal to run an executable file in the current directory:
  4. Copy to the directory, and change the target filename to remove the extension. Note that we will need to use as the directory is not owned by our user:
  5. Run the new executable, , to test that it works as expected.

Now that you've created and executed Bash scripts, it's time to explore more advanced topics. Happy scripting!

Stay on the cutting edge: Sign up for the Tom's Hardware newsletter for the latest technology news and in-depth reviews!

  1. In the world of home-and-garden, one could implement a Bash script to automate watering the garden based on weather data accumulated from cloud-computing services, enhancing the lifestyle efficiency and convenience.
  2. To make a home-and-garden monitoring system more sophisticated, incorporating technology like a Raspberry Pi and a set of sensors to track soil moisture, temperature, and sunlight levels can be combined with Bash scripts for a comprehensive and intelligent home-and-garden management solution, resulting in an enhanced lifestyle experience.

Read also:

    Latest