Basics of Bash

Topics of this blog:

Erick Villeda
4 min readApr 25, 2021

ECHO, variables and user input, if then/else statements (if then else fi/ elif), and flag shorthands to compare variables or check file conditions.

What is Bash? Bash is the shell, or command language interpreter, for the GNU operating system. The name is an acronym for the ‘ Bourne-Again SHell ‘, a pun on Stephen Bourne, the author of the direct ancestor of the current Unix shell sh , which appeared in the Seventh Edition Bell Labs Research version of Unix.

Bash Description

Intro

Generally speaking, bash is the standard terminal shell for Mac and Linux, typically used for scripting or navigating through out your computer. Today I will be talking more specifically about scripting with bash.

Just to set things up let us begin with how to execute a bash script. Once you’ve made a directory with mkdir cd into said file and open with your text editor.

We also need to make this file executable because as default you will most likely get a permissions denied error.

To make a file executable we need to run the chmod(change mode) +(plus/add)x(executable permission ) commands. Adding our filename at the end of the command to show which file we’d like to make executable. In my case being bashbasic.sh . Full command chmod +x bashbasic.sh

Now, to execute the file we just need to run ./filename in my case being ./bashbasic.sh .

Finally, we need to declare which dialect we are using within this script; to do so, we first need to find where our bash in located by running which bash . In my computer and typically for Mac it will be located within /bin/bash . So to declare this file as a bash script at the top of the file we put a shibang. Which is made up of a #(sharp) and a !(bang) also known as a shibang followed by the location of bash on your system.

This is what your file should look like before we begin.

Echo

Echo will print what ever you place after it including text and variables to the terminal as shown below.

Setting Variables and User Input

Assigning variables is as easy as in most languages only that there are caveats. Firstly, being that convention is uppercase for variables, you can use lower case variables and it will work; but uppercase is industry standard for bash scripts. Secondly, assigning is done with an = . Finally allowed in variables are letters, numbers, and underscores also known as snake case.

Interpolation with $ or ${ }

To obtain a users input we must prompt them and tell the computer to read their entry. Keyword read and we’ll use the -p flag to prompt the users response.

If Statements

A basic for any Software Programmer is an if/if-else/else-if(elif) statement, below I will demonstrate all three at once. As the script is read the variable will go through every condition to check if it is true or not. If true, it will then execute the echo. If not true it will continue through the code until a condition is met. Lastly and key to the if statement specific to bash is they are closed with a backwards if(fi).

Flags

Below is a cheat sheet for bash shorthands/flags.

Conclusion

Bash is a very useful and common type scripting language that everyone from developers to IT should show, as it make repetitive tasks much easier. I had so much fun learning and can’t wait to expand my knowledge and share it! I hope you’ve learned with me and always remember to continue coding!

--

--