Are you able to automate with Bash scripts? Definitely! Is this something you want to do all the time? Of course not! It depends on the situation.
What is it that you are automating? If you are a System Administrator, SRE or a frequent user of Linux, Bash scripts come in handy.
There are things you come across where you repeat tasks over and over again. When you come across such repetitive tasks, you should ask yourself “Is this something I want to do again and again?” No, it’s not. You can automate it.
You don’t have to repeat typing the same command over and over again. You are not a monkey! It is fun to do it one. Twice okay. Three times, you automate.
What is Bash?
Bash is a Unix shell and command language. It is the default login shell for most Linux distributions.
What shell am I using?
To find out what shell you are using, open a terminal and run:
$ echo $SHELL /bin/bash
As shown above, the shell is bash. You can also confirm by checking the current process of your shell. You can do this by running:
$ ps -p $$ PID TTY TIME CMD 458 ttys005 0:00.03 -bash
What should I automate?
There are many things you can automate with Bash scripts. The list is endless. It is up to your imagination of what you can automate.
Here are some examples of what you can automate:
- Copying files from one location to another at a set time.
- Searching for some text in a log file everyday.
- Renaming filenames in multiple folders multiple times.
- Running a daily program and parsing the output.
- Searching multiple config files and editing the text in those files.
What about automating in compiled languages?
Scripts are written in ASCII text format so it is very easy to debug. You can write and test it much more quickly than compiled languages. You get the job done and can get on to the next task.
Should I always automate with Bash scripts?
Of course not. You should always say never say always. There are always exceptions to the rule. It depends on what you are automating. If you are automating a backup solution with retries, redundancy, fallback, and multiple paths, don’t use Bash scripts.
You are now writing a huge program. Whenever you are designing a solution that will take hundreds or thousands of lines of code, it’s time to switch to a compiled language. You want a robust programming language that can handle all of these things.
Bash scripts are good for quick and simple processing of tasks and don’t get too complicated. If you think it’s going to be complicated, it’s probably going to be complicated.
What Does $2 Mean in a Bash Shell Script?
The name of the script itself is $0. The first argument is $1. The second argument is $2. The third argument is $3.
As an example, let us assume you are running this command:
$ compare_text_files.sh file1 file2
In the example above, compare_text_file.sh is $0, file1 is $1, and file2 is $2.