Pretty much every normal task you need to perform on Linux should be possible through point-and-click on the graphical interface. There are occasions when you need to use the command line or terminal window, and that’s normally when something’s gone wrong or isn’t working the way it should do.
The command line isn’t just a fix when your computer isn’t working right. It can – with practice – allow you to automate complex jobs, saving you time.
You can take a bunch of commands you’d type on the command line and put them together in one file. On Windows thats’s called a batch file, on Linux it’s a shell script.
Shell scripting can do a lot more than just automating tasks – it’s a powerful programming language in its own right. It has limitations – principally being relatively slow and not being appropriate for most advanced programming tasks – but there’s a surprising amount you can do with it, with support for loops, variables, arrays and more.
Your first shell script – hello world!
Tradition dictates that your first programme in a new language should print “Hello world!” to the screen.
To do this you will need:
1. A simple text editor. Here I’m using kwrite but there are hundreds of others. Do not use a word processor like LibreOffice, though – keep it simple.
2. A terminal to give you access to the command line
3. A folder to put your scripts in (any one is fine, just decide where).
Create your script – open the text editor and type this, then save it calling the file hello.sh (you can call it whatever you like, but it’s a good idea to put .sh at the end to remind you it’s a shell script).
Let’s look at this simple script line by line. The first line is
That should always be the first line of every shell script you write – it tells Linux that it’s a shell script. (The name “bash” is itself a pun – there are lots of different shells that work in slightly different ways and one popular one was called the “bourne shell” or bsh. Bash is short for “bourne-again shell” and is a free alternative that’s become the Linux standard).
The second line contains the text of the program.
echo “Hello World!” simply displays “Hello World!” (without the quotes) the screen.
I’ve included two comments, both of which are entirely optional. In shell scripting, the pound sign # denotes a comment. Anything that appears after the # on a line is a comment and will be ignored by the computer. When you write scripts, the comments are there to remind you, or tell others, what you’ve done and why. Ascript that seems obvious when you write it can be impenetrable six months later without comments.
Now it’s time to run your first script, but there’s something you have to do first.
From your terminal window, use the cd command (change directory) to move to the folder you’ve saved your script in.
To run a script, you need it to be executable, which you can do with the command chmod +x, followed by the script name.
Use the command ls -l to check: if it’s executable. Look at the line for your script. The fourth character will be an x if it’s executable and a – (dash) if it isn’t.
Now for the moment of truth. To run your script, simple type ./ followed by its name.
All being well, the words “Hello World!” have now appeared.
That probably seemed like a lot of effort just to type “Hello World!” – and it was. But those are the basic steps for every shell script: write it in a text editor, save it, make it executable and run it.
Your second script – add two numbers together
If you thought programming couldn’t get any more exciting than “Hello World!”, prepare to be amazed because we’re going to write a script that adds any two numbers together before your very eyes. When the script’s run, the user will be asked to enter two numbers, then the script will give the total.
To achieve this astonishing feat, we’re going to need variables. You probably remember variables from math at school – x, y and z could be different numbers.
In shell scripting a variable can represent a number, word, sentence – almost anything.
So let’s take a look at the script.
It starts off with #!/bin/bash and then there’s a comment – you know about those.
The next two echo commands print out to the screen, prompting the first number to be entered.
Next we have read x. This tells the script to wait for the user to type something, and assign whatever’s typed to the variable x. After the next echo there’s read y. Again the script pauses and waits for a number to be entered, but this time the number is assigned to the variable y.
The let command is the way the shell script does math. Here it adds together the two numbers the user has entered and assigns the total to a variable called z, but we could have told it to subtract, divide, multiply or any other common operation.
Finally, the last echo command prints out the sum, including the answer. Note that the variables all have a dollar symbol before them – instead of x,y and z we have $x, $y and $z. When you assign a value to a variable, you just use the variable name on its own. When you want to find out what the value is, you include the $.
Run the script as before:
Think of variables as boxes. The variable name is on the lid and there’s something in the box.
Here’s another example:
let a=10 # put the value 10 in the box a
b=”harry” # put the value harry in the box b – note that we only use let to assign a number
let a=a+15 # take whatever’s in box a, add 15 to it and make that the new value in box a
echo “$b is $a years old”
harry is 25 years old
These simple shell scripts are just the tip of the iceberg – amazing things can be achieved with scripting!