The command line can be an incredibly useful tool, allowing for quick navigation of directories, launching apps/executables, and a plethora of other tasks. However for all the use cases that it contains, it can be difficult to keep track of all the different commands, let alone have to repeat them often. Cases when someone needs to repeat a series of command line commands can be time consuming and tedious. Luckily there is a tool in our tool belt that will allow anyone to automate this process. This tool is called a BASH script.
BASH stands for Bourne Again SHell, and is in its essence a command line interpreter that interprets user commands and allows us to carry out different actions. We can use this to our advantage by creating a .bash file and entering the commands that we want to run within that file. It really is that simple. Once we have the file with all the commands that we want to run saved, we can go back into the command line and run it. This will then execute each command in the bash file one by one until it runs through them all, at which point the command line will be ready to accept another command.
But what if you need to loop through certain commands? Bash scripting allows for loops to be written directly within the file, and supports a myriad of loop types. Such as for-loops, while-loops, and until-loops. You can also control the exit conditions of these loops with pre-set ranges, breakpoints, or good old enumeration. If-statements are also supported, and work in a very similar way to traditional programming languages. In reality bash scripting is its own kind if programming language. One that focuses on executing command line commands. Many things that you can do in most high and low level languages you can do in a bash script. You can even write individual functions in a bash script and have them execute only if specific conditions are met. Just like in a normal programming language like Java or Python
Given all this one can easily see how a bash script can be so incredibly versatile. From simple clusters of commands, to complex functions with loops and conditional statements bash scripting gives anyone the tools they need to get the job done. Being able to automate different command line tasks can save time, and being able to do so in a complex manner opens up the door to intricate automation scripts that can in some cases remove the need for the user to even interact with the command line at all.
https://ryanstutorials.net/bash-scripting-tutorial/bash-script.php
https://ryanstutorials.net/bash-scripting-tutorial/bash-loops.php
https://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php
https://ryanstutorials.net/bash-scripting-tutorial/bash-functions.php
From the blog CS@Worcester – Sebastian's CS Blog by sserafin1 and used with permission of the author. All other rights reserved by the author.