Introduction to bash scripting

If you use a Linux or Mac computer, or if you are a developer, chances are you will work with the terminal at some point. The most common command line application that is used is bash. This post is to get you familiarized with one of the most powerful features of bash which is bash scripting.

Why make bash scripts?

There are many reasons why bash scripting is so strong. For starters, if there is a task that you find yourself doing over and over again until it becomes tedious, you could create a bash script that would do it for you in an instant. Why repeat the same work when you could automate it? And the best part is that it’s not as hard as it may sound. This post will attempt to get you to create your first bash script, and to understand how it works.

Let’s start:

I am going to assume that you have access to a bash terminal window. If you are using Windows you might want to either install a bash for windows program, or to run a Linux Operating System through Virtual Box. Anyhow, once you in bash, go ahead and create a new file with the editor of your choice. You can use vim, nano, or even a gui editor if you would like. just make sure you are making the file in the same directory where your bash terminal currently is. You can check your current directory with the commands

$ pwd

I am going to create a new file using the vim editor


Note that bash script files don’t need an extension. By convention we use .sh to easily tell it’s a bash script file, but I may as well have named the file mybashscript and it would have made not difference.

once the editor is open, and we are ready to start typing (if you are using vim to edit make sure you press “i” to enter insert mode), you want to make sure the first line of the file is


This line tells your computer that the content of this file is a bash script (hence why no file extension is needed).

Go ahead and press enter once or twice and type:

echo "This is my first script"

Now your entire file should look like the following

echo "This is my first script"

Now save the file (if you are using vim you want to press   esc  then  :  followed by wq)

Once you exit the editor, make sure the file was created by typing the command:

$ ls

which the files sitting in current directory.

You should be able to see the file named

At this point we are not ready to execute the script yet, and this is because whenever you create a new file, by default it will not have execute permission. To add execute permission to the file we can use the chmod command:

$ chmod +x

This will make it so that now we can execute the script file.

To run the file, we have to type into the terminal


The reason we have to type ./ in front of the file name is that bash doesn’t know that is a global command. When you make a script you have the option of making your script a command that you can run from anywhere just like the command ls, or pwd. By typing the ./ we are telling bash that the file we want to execute is located in the current working directory which is what the dot in (./) stands for. After typing ./ into the terminal and pressing enter, you will notice that the text (This is my first script) appears in the terminal which signifies our script ran successfully and did what we asked it to do. Our script is a very basic script that only does one thing (print some text to the screen), but we could have added more commands to the script which would have executed sequentially one by one. Congratulations, you just made and ran your first bash script.

One more thing. If you want your script to be recognized as a global command so that you can just type into the terminal from any directory and it executes properly, you will have to add the directory that contains the script file to the $PATH environment variable and this is something that will be covered in a future post.

If you liked this tutorial or if you have any feedback, please leave a comment.


How to check your computer specifications on Linux

If you would like to check your computer specs via a command window (terminal), there are several commands and options available for you.

sudo lshw

The previous command stands for ‘list hardware’ and it does exactly what it stands for. It lists all your hardware including the memory, CPU, mother board and the rest. Since this command gives so much information you will have to scroll up to find the bits of info you care about. You could always use the less command to scroll through the info:

sudo lshw | less

This way you can use the up/down arrow, pg-up/pg-down keys, or the middle mouse button to scroll through the specs.

Another option is to use the html flag (-html) in conjunction with output redirection to store the specs into a file. That way the specs are saved in a file you can access easily anytime.

sudo lshw -html > mySpecs.html

If you just want to look at the available memory, you can use the free command with the -h (human flag) to make the output easier for humans to understand:

free -h

Alternatively, there are files inside the /proc/ directory that contain hardware specs for your machine. Of particular interest are the /proc/meminfo file and the /proc/cpuinfo file. You can look at their contents the using the cat command. (cat displays the contents of a file onto the terminal).

cat /proc/meminfo

cat /proc/cpuinfo

meminfo contains detailed information about the memory. The top three lines of the output are the most important ones. They show MemTotal, MemFree, MemAvailable. Keep in mind these are shown in KB so you would have to divide by 1,000,000 to convert to GB.

cpuinfo contains a list of each CPU core available to your computer and information for each one.

Other alternatives of getting memory information include the commands


Checking video graphics card specs

sudo lshw -c video

This will display an output like the following

       description: VGA compatible controller
       product: GT215GLM [Quadro FX 1800M]
       vendor: NVIDIA Corporation
       physical id: 0
       bus info: pci@0000:01:00.0
       version: a2
       width: 64 bits
       clock: 33MHz
       capabilities: pm msi pciexpress vga_controller bus_master cap_list rom
       configuration: driver=nvidia latency=0
       resources: irq:33 memory:e2000000-e2ffffff memory:d0000000-dfffffff memory:e0000000-e1ffffff ioport:7000(size=128) memory:c0000-dffff


Another Way



lscpci | grep VGA

Checking USB devices




Hopefully these commands will suffice your needs for the information you need to get about your system. Leave comments if you feel I left something important out, if I should make changes, or if there are any errors. You can also comment for any other reason 🙂