Linux Mint Shell Scripting Primer : Part I

[ This is a five part series that covers the very basics of Linux shell scripting. ]

The most powerful thing Linux offers is the shell. The shell is a command line interpreter similar to the command prompt in Windows. It helps in managing processes, run programs and tons of other important stuff. The command prompt is the interactive part of the shell. When you copy files, delete directories, check if a system is working or not through various commands, shell does that for you.

Where there is shell, there are shell scripts. Simply put, scripts are tiny programs that do what you want them to do thereby freeing you from doing repetitive tasks. Scripts can be anything like checking an IP address of your computer, finding out what the free disk space is in your Linux system or even generating various system based reports.

The First Linux Shell Script

Let’s see how to get started with Linux shell scripting using Linux Mint.

What you will need is a Linux system 🙂 and a cool text editor. Bluefish editor is a really cool one for this. Examples in this tutorial series use Linux Mint system and Bluefish editor to write the scripts.

The first shell script :

Finding the computer name in Linux Mint

The first line in above example or any Linux shell script (#!/bin/bash in this case) tells the shell which shell to use to run the script. This is called a ‘shebang’. So #!/bin/bash specifies bash shell to be used for executing this script.

The next two lines are all comments, anything worth mentioning that gives info about the script should be included by first typing #.

Finally, the command that does the job of finding the Linux Mint computer name is entered on last line – hostname.

That’s it ! You have made your first Linux Mint shell script. Now to make it work, we first save it with a relevant name like sysname.sh

To make it executable so that it can run, type in chmod a+x sysname.sh

One thing to note is that by default if you are in directory A and your script is in directory B, it won’t execute because it won’t be found. To fix this, either define the path of your script in the $PATH using export command or simply execute the script from the directory it is present in.

You can add the following lines in the .bash_profile to export the path where your scripts are stored :

export PATH=$PATH:/path/to/my/scriptsfolder

The script sysname.sh is saved on my desktop, so I will execute it from my Desktop folder with the command ./sysname.sh

Voila, it works ! What the script did was what we wanted it to do, list the name of the current computer. In short, to do something using a Linux script, first write the script, save it somewhere and then make it executable. After that, run it and be overjoyed 🙂

Stay tuned for part II.

[Update : Here are all the parts II, III, IV and V.]

Comments are closed.