What Is Bash Scripting?


A script works like a mini-program you can run when you want a quick way to perform your most frequently used administrative tasks with Bash. Most Linux administrators have several in their hip pockets, for the very simple reason that it makes their lives easier when performing jobs like backing up files. Their files are creating using their favorite plain-text editor like Pluma. All Bash scripts should have the following:

  • They should start with the line, #!/bin/bash. This allows Bash to recognize the file as one that can be run in Terminal. The #! part is called shbang and /bin/bash is the full path to the shell and the Bash executable.

  • They should be executable. Once you have your script created, you should make it executable right away so you can test it for errors and you aren't wondering why you can't run your scripts. The command for making it executable is chmod u+x yourbash.

When creating your executables, remember that anything that can be done in Terminal can go into a Bash script. So be careful and especially pay attention to the outputs whenever you run one, because they can be altered if somebody got curious.

What Can You Do With Bash Scripting?

The short answer would be, “Anything you can do with Linux Terminal, you can do with Bash scripting.” However, scripts can get to be pretty complex. For some tasks, a 30-line executable file is considered short. If you find yourself frequently using any of the following in your command line, you might want to consider moving some of them over to a file.


Bash loop

Loops are useful whenever you want Terminal to do something until it triggers the condition you set. I've found it useful to set most of my variables before I initiate the loop, especially if I want to set a starting value for them.

The While loop is useful for doing a task while something is true; for instance, add a series of numbers while the total is less than 10. If you want to do something until a condition is true, use the Until loop. Again, until is useful when you want the loop to stop when it gets to a certain point. The For loop is handy for cases in which you want Bash to perform a task for each instance that matches a condition you set. I use For quite a bit for counting the number of items that meet a specific condition.

Tasks That Require User Input

Bash user input

Do you collect a lot of data from people who have limited experience with Terminal? Sure, you could employ a lot of expensive data-collection software, or you can just use Terminal to prompt users for input and then use Bash to run the results when you're done. It's not fancy, but it gets the job done. The read command is used to capture user input and set it as the value of a variable, and then you can use if statements and loops to tell Bash what to do with the data.

Tasks That Are Complex or Require Long Inputs


Bash complex

Scripts are useful for handling multi-step operations and cases where you get sick of typing the same long command on a frequent basis. Gather those operations into a series of scripts, and you can handle your frequent administrative tasks with less typing and fewer typos to mutter over.

A Resource To Check Out





Test your scripting skills with the UNIX Academy! If you're looking for employment as a Linux administrator and/or going for your Linux+ exam, this is a good way to test your strengths and weaknesses with Bash and scripting.