What is Bash (Unix Shell)?
By: Website Developer
Tags: Programming Languages, Bash Code, Seattle Programmer, Bash Programming

Bourne Again Shell, most commonly known as Bash, is a command language and Unix shell. It runs in a text window and can run simple commands to do certain actions. It’s used to read and execute commands from a shell script. A file that can contain code from a variety of languages like Python or Java.

History

Bash was created in 1989 by Brian Fox. Under the GNU project, Richard Stallman founded the Free Software Foundation who wished to create free software. The Free Software Foundation allowed the public access to many programs. Fox started working on Bash in 1988 after the previous programmer took too long on the project. Fox worked on the program until between 1992 and 1994 after being laid off and replaced by Chet Ramey.

In 2017, Microsoft worked with Linux developers to add Bash to Windows. This allowed developers to be able to run Linux commands on Windows. Unfortunately on June 3, 2019, Apple decided to change its default shell from Bash to zsh. It is believed Apple did this due to the age and having to use an old version of bash.

Platforms

Bash has since become the default login shell for many Linux distributions. It was used on Macs until October 2019 after the Catalina update. There is also a port that lets Windows and Android users have access to Bash. These recent ports were created to bring the benefits of Linux to other platforms.

Due to being the default shell on Linux, Bash continues to grow amongst consumers who are switching over to Linux. The chart below shows the market share of operating systems used by computers.

The chart shows how in July 2019, 7.05% of all computers ran Linux. This allows Bash to be widely used amongst the Linux community and continue to grow. Also, the addition of Bash to Windows 10 allows a lot of consumers access to the language.

Benefits

Bash is widely available and can be accessed by many platforms. Bash is able to use command-line completion which allows the program to auto-complete what they’re typing. Developers used this to type variables, something that stores a value like a number, and file names. Bash is also used to search through the files in a computer and execute certain programs.

Bash has many features and allows users to do many things. You can search through your computer to find files and folders, run a program, and work with many files. Bash is not only a language that is used by professionals but consumers as well.

You can also use “?” to determine the success of a program. “?” is a built-in variable that is used often. Below is an example of the use of “?”.

In the fourth line, the program is told to “echo $?” which means show us what the variable “?” is. We see that “?” equals zero which means the program, in general, was able to run without errors. If “?” equaled any number other than zero, this means there is an error that needs to be solved.

Another thing to mention is that Bash is one of the highest paying languages amongst programmers. Below is a graph showing the median pay based on the language used.

The graph shows Bash developers make about $100,000 a year. This may lead you to believe that the pay of these developers is high due to the demand for Bash developers. Yet, the high pay maybe because of the age of Bash developers. The graph below will show the age breakdown of programmers in many languages.

This chart shows the age of Bash developers in comparison to other languages. Developers that work on Bash are more likely to be older and in turn, more experienced. In fact, of the 11,000 respondents, none were under the age of 29. This may mean because Bash developers are older, they have more experience in programming which leads to them having higher pay.

Downsides

Bash is rarely used to create a program but to pair with programs created in different languages. The time it takes to execute commands is also seen as slow in comparison to different languages. The code may be unreadable to other programmers and hard to understand. Errors are more common and cause developers to take extra care when developing.

The age of the language also causes the code to seem hard to read causing a steep learning curve. This is in comparison to more modern languages that focus heavily on readability.

It’s also recommended to not write long programs because that is not the primary function of the language. Quick commands or short programs is what most people use Bash for. If it is necessary to run a long program, you should use another language like Python or Java. Also, if performance is important for the program being created, you should use another language. Bash is slow compared to other languages meaning it should not be used as the main language.

Examples of Code

If Statement:

If statements are used in programming to do something if something is true. The basic structure of an if statement is below.

$ if [ <some test> ]

$ then

$ <commands>

$ fi

On the first line, we see what the condition is. Between the brackets, there must be a statement that is either true or false. This can be whether a number is greater than another or if a variable equals a number. Line three is where you write what you want the program to do like make it say something. Lastly, when it says “fi” that means that is the end of the if statement. Below is an example of an if statement that determines whether the person is of age to drink alcohol.

$ #!/bin/bash

$ # Basic if statement

$

$ if [ $1 -gt 20 ]

$ then

$ echo That means you are old enough to drink.

$ pwd

$ fi

We see in this example on line 4 the condition. It checks if the number from the first line, accessed from a file, is greater than 20. If the number is greater than 20 then we go to line 6 where it will say the quote. Once executed or if the condition is false the “fi” tells the computer the end of the if statement.

Loop:

A loop statement is used if you want to repeat a command or when something is true. There are two types of loops: a for loop and a while loop. Below is an example of a for loop.

$ for i in {0..10..2}

$ do

$ echo “Welcome $i times”

$ done

In the example, the first line is the start of the for loop. The “i” in the first line is a variable we are creating that will be used in this loop. The braces show the value “i” will be assigned to. The first number is the initial value of “i” at the beginning of the loop. The second number is the last value of “i” and stops the loop when “i” equals that number. Lastly, the last number is what number will add to “i” after each time through the loop. In the example “i” will start at 0, end at 10, and go up by 2. The third line shows the command the program will do which is say “Welcome $i times”. If “i” equals 2, it would say “Welcome 2 times”. The last line shows the end of the for loop by writing “done”.

$ i=0

$

$ while [ $i -le 2 ]

$ do

$ echo Number: $i

$ ((i++))

$ done

In the while loop, the first line shows assigning the letter “i” with a value of “0”. The third line is the start of the while loop. In the brackets, it means to do the command while “i” is less than 2. The fifth line is the command, and it tells the program to say “Number: $i”. If “i” equals 1 it would say “Number: 1”. The sixth line shows that to set the value of “i” up by one. Unlike the for loop, you need to increase the value of “i” by writing “((i++))”. The last line shows the end of the while loop allowing the program to continue.

Files:

Bash is commonly used to deal with files stored in your computer. It can do many things involving files. Below is an example of creating a file.

$ #!/bin/bash

$ echo -n “Enter directory name ->”

$ read dir

$ if [ -d “$dir” ]

$ then

$ echo “Directory exists”

$ else

$ mkdir $dir`

$ echo “Directory created”

$ fi

In the example, we see a program used to create a directory. On line 2, it asks the user the name of a directory and assign it to the variable “dir”. Then it checks if there is a directory with that name already. If there is it will say “Directory exists”. If the directory does not exist, then it will create a directory with that name. It does this by writing “mkdir” then the name of the directory. Finally, it will say “Directory created” and end the if statement. Below is an example of deleting a directory.

$ #!/bin/bash

$ echo -n “Enter filename ->”

$ read name

$ rm -i $name

$ echo “File Deleted”

In the example, we see a program used to delete a file. On line 2, it asks the user the name of the file to delete. It will store that in the variable named “name”. Then, by typing “rm -i” and the name it will delete the file. Finally, it will say “File Deleted” and end the program.

Trends

The popularity of Bash grew as more and more consumers started using Linux. Many consumers had to learn how to use Bash to access their files, download programs, and more. The popularity also grew due to being the former default shell on macOS devices until being replaced in 2019.

But Bash is not as often used amongst professionals in the community. We see below a chart showing the percentage of programming problems solved in Bash on codeeval.com.

This chart shows in codeeval.com, a competitive programming website, only 0.4% of problems are solved with Bash. This means that Bash is one of the least used languages in the professional world. The popularity of the language with consumers continues to rise with the popularity of Linux.

Bugs

On September 12, 2014, Stéphane Chazelas told Chet Ramey of a bug in Bash. The bug allowed hackers to cause the computer to run commands. This bug was called “Shellshock” and was quickly updated on September 24. Once the update was sent out, an announcement was made of the bug report and what it could do. This led hackers to create programs to attack computers that had not yet updated Bash. The vulnerable devices from “Shellshock” was one of the highest due to a lot of computers with Bash.

Takeaway

Bash is a widely used language on many devices. Due to being free software, it allowed many developers to have access to a shell. It is still widely used by Linux users and will continue to thrive in the technological future ahead. Although it may not be used highly amongst professionals, the consumer market will continue to grow.