BASHing the Linux Shell

The famous Bourne Again Shell, named after the Jason Bourne series, is the foundation of the Linux experience. The black screen looking back at you is what everyone thinks of when they think of Linux. It can be very intimidating to start, but also incredibly powerful. There are a lot of things that can be done with just a few letters. For example, rm -rf is great for cleaning your harddrive, and probably taking your future along with all the deleted data. The rm function is used to delete files in terminal. It can take arguments including r and f, which tell it to recursively delete everything without asking the user for permission to proceed whenever it comes across something sensitive. Forcefully recursively deleting stuff can be useful in large and obsolete directories where confirmations would needlessly slow things down. Also, all arguments or flags are preceded by a slash when they are used with BASH or terminal commands.

The ability to easily automate menial tasks and interact with the operating system makes Linux a powerful tool. A while back I wrote a script that reads a scheduling file, and if the user wants allows them to update the file too. It takes all of their input from the terminal and opens up appropriate programs if required. It is not very long or complicated at all. Further, this code analysis could be helpful for anyone preparing for CompTIA certification. While BASH is not a cornerstone on any of the exams, scripting does lightly come up from what I recall. I also recall BASH commands coming up from time to time, and understanding the syntax might make memorizing those commands easier. I know it helps me to relate everything to programming, but hopefully somehow this helps you too.

This first block of code is just saying that traditionally you start bash scripts with a "shebang." For some reason it was giving me errors when I ran the script in Ubuntu so I commented it out. Because I figured that is more appropriate than to just remove the line altogether. The next block of code will actually contribute to the operation of the script.

Start of a BASH script

The echo command is how output is generated in BASH scripts. The syntax is the command echo followed by the string of output in quotation marks. Echo by itself will produce nothing, just a blank line. It can be a quick and easy way to give some space in the terminal. Which can be helpful for outputting information from a file.

cat function to read the start of the file

Since echo handles output, it can be used to initiate questions from the user. However, where BASH differs from a lot of languages is how it handles variables. The variable answer has not been initiated or defined up to now. Also, to get input from the user the command "read" is used. Think of it as reading input from the terminal and storing it into "answer." Now that the user's answer is being captured something needs to be done if the user wants to access the file.

Code for user interaction

To start out, aside from the use of brackets instead of parentheses, referencing answer with a dollar sign, and the lack of a semicolon there is nothing shocking here in the world of programming. An "if statement" simply executes some command if a condition is met. Further, the weird double equals signs (==) just checks for equivalence. It allows the programmer to ask, "does answer equal y or Y?". The double pipes in between the sequence of brackets covers the "or" in this statement. Lastly, variables in BASH are referenced with a $ sign. Outside of that there is really nothing exciting here. It is just saying if the user types y or Y into terminal back up the file and open up the file in emacs.

Code to back up and open the file depending on user input

In order to back up a file in Linux using the command line files are copied (cp). A command file extension for backups are the self explanitory.bak extension. The way cp works is that you specify the file you want to target and then where a copy of that file will be sent, and in this case its new extension too so the operation is not redundant. The command "mv" has a similar syntax, but only moves the file to the new location. Of course, if they type in anything other than y or Y the program will just terminate. The fascinating part about BASH though is how it terminates if statements.

Either open the file or just quit the app

A lot of programming languages encapsulate if statements in curly brackets. The designers of BASH had a different idea though, and instead went with 'fi' which is the reversal of 'if.' A very fascinating way of avoiding the use of curly brackets. The final line is really nothing special for anyone familiar with low level programming.

'fi' which terminates a bash if-statement block

All this next line does is tells the shell to 'exit' or terminate the program. A code of 0 typically means that everything worked out just fine. Pretty neat script I think for updating and keeping track of a file. I ended up trying to build the same thing in Python with a GUI, and that's still a work in progress.

As easy as BASH can be to use, it should not be used to manipulate sensitive data. The source code is pretty easily accessible and it is not that hard to read for humans. It also lacks a lot of other features that more robust and standardized languages feature. However, if there are common tasks or sequences of tasks you're doing regularly it is a very powerful tool. Lastly, you can view the the source code with a detailed readme on installation on Github, or download the script here. This script comes with no guarantees regarding functionality or warranty, but it should work on any Linux distro. I am very sure I ran it just fine on Ubuntu 20.04.