J. R. Swab

Learn Bash: Tutorial 001

Categories: [Technology]
Tags: [learn], [open-source], [linux], [bash]

This is a start to a series all about bash and scripting for the terminal. There are lots of commands to cover but today is all about the basics. As long as the information fits within the one thousand word limit that this blog adheres to we should cover what bash is and some basic commands. The goal is to get you moving around the terminal and know what a bash script is by the end of this post.

Bash is an acronym for "Bourne Again SHell" because it is the open source response to the original "Bourne Shell" used on many old UNIX systems. Brian Fox created bash while working for the Free Software Foundation in the 1980s. We can now find this scripting language on (almost) all Linux distributions and Mac OS (sorry Windows fans you'll need an emulator).

A while back we talked about the terminal and why it is an awesome tool. Bash runs in the terminal and allows the user to execute many commands via a script. This saves us a lot of time when using the terminal. Especially when we have a complex or long string of commands to run each day.

As an example, we can create a script to run as soon as the computer finishes loading. In said script we can run programs we always want running in the background. Dropbox and f.lux are great examples of programs that need to be running all the time. If we have to execute a command every time we turn on our computer, it will get old.

The Basics

As mentioned above, a bash script is also great for complex commands or a long string of commands. Let's say we want need to run the same series of Git commands several times an hour. We can save time by using a bash script and writing all the commands out once within the file. This saves time upon each use since now instead of running four or five commands we only need to run one.

Here is an example of what a bash script looks like:

#! /bin/bash
STRING="This is a simple bash script."
echo $STRING

So what does this do? Once the script runs in the terminal, we will see "This is a simple bash script" on the next line down. This is not useful but is a good way to show the format of the script. As with all programming and scripting languages if some part of the formatting is incorrect we will either not get the results we desire or the code will not run at all.

#! /bin/bash is what tells the terminal what type of script we are asking it to run and should be a part of every bash file. STRING is a variable that holds the information between the quotations. Any time we use STRING in our file bash will interpret that as the text that follows. Finally echo, this is the command that tells the terminal to show on screen whatever follows. In our case it is the variable we mentioned earlier.

That is how we set up the bash file and add commands. When saving the file we can call it anything we want so we can remember what that script does. There is no need for an extension if you are using a Linux distribution. However, it may be useful to use .sh to the end as a reminder that the file is a shell script.

Now we will have to tell the computer to make our file executable. To do this, we need to run a command in the terminal. The command is chmod +x [file]. Also, if we can not run the script after making it executable we need to run chmod 755 [file]. This tells the computer to let us run, read, and edit the file. While everyone else can only run and read. There are many other number combination we can not get into now so search "chmod permissions" if you want a different set.

Easy Shell Commands

The more we use the terminal the more we memorize these commands. They became muscle memory as many people discover after switching to a new keyboard layout such as Dvorak. Perfect practice makes perfect so we need to make sure we get all the basics correct.

First what is a command? A command in a string (like a sentence) of letters, numbers, and symbols that tell the computer to do a specific task. The most basic and most common command is cd.

This is an acronym for "change directory" and it does just that. However, we need to tell it what director we want to change to. If we do not specify a folder, the command will take us back to our home directory. The command will look as follows:

cd .. or cd Downloads/

The first command will take us up one directory. Think of this like a back button. We don't say "back one directory" because the computer's file system has a tree like structure. The second command above will take us to the "Downloads" directory. Awesome, but how do we see what is in this directory?

ls

This command stands for "list' and it will display all non-hidden files and folders our current directory. To veiw everything, including the hidden files, we need to add an argument. Arguments (almost) always follow directly after the command. If we want to see the directory's hidden files, we need to use the command "a" argument. Make sure to add - (dash) before the argument.

ls -a

Great! We can now move around the terminal. Doing so is the absolute basic step in using the terminal and creating bash scripts. The reason we need to know this is that bash needs to move around just as we would if running the commands by hand. When we create a script we are saving time later by typing the command sequence once in the file. Instead of every time we need to execute the task.

What we covered here are the fundamental blocks in building the proper bash scripting knowledge. In the next post we will cover more about the bash language as used in the executable file. The topics will focus on variables, strings, if-than statements, and a whole slew of other awesome stuff. All to make our computer do epic things fast than we ever could.