Bash Shell Scripting

A shell script is a code (script) written for the shell or command line interpreter of an operating system such as Unix. It allows users to automate tasks which would be cumbersome to execute manually. For example renaming 2000 files. You could do it by 2000 mv commands or write up a 3 line shell script to do it for you.

A shell script can be written in any shell scripting language installed on the system. The most widely used shell is the Bash Shell. Others include csh, ksh, tcsh, etc. Here we would only be discussing bash shell.

Getting Started

Which shell I am running?

echo $SHELL

This command would print the name of the shell you are running.

How do I switch to Bash Shell?

bash

Which shells are installed on my system?

cat /etc/shells

First Example

clear; echo "This months calendar"; cal;

A shell can script can be run on commandline from a file. You can use any text editor which doesn't insert formatting into text to write a shell script. vi and emacs are good editors. Save the following code in file called test.sh:

clear
echo "This year's calendar"
cal -y

By convention, shell scripts are saved with .sh extension. Change permissions of the file:

chmod 755 test.sh

You can use any one of the following commands to execute your bash script.

bash test.sh
sh test.sh
./test.sh

Bash Variables

Bash uses two kinds of variables

  • System Variables: Variables pertaining to the Unix system
  • User Defined Variables: Variables created by users for scripting

To see a list of system variables type:

set

This would give you a list of system variables. DO NOT change values of any of these variable unless you know what you are doing. Unlike Microsoft windows, Linux allows you more control over you system assuming that you know what you are doing. So, if the root granted you a rather unrestrictive access, you can create many problems for yourself.

To create a user defined variable 'number', for example:

number=10

You can use alphanumeric characters and underscore to for variable names. Variable names are case-sensitive. You many not use spaces in assignment. Following are not allowed.

number =10
number= 10
number = 10

To assign NULL values:

budget=NULL
budget=NULL

To print variables

echo $number

Example

year="427 BC"
name=Plato
city=Athens
echo "$name was born in $city around $year"

Renaming Files in Directory

Suppose you have 2000 photos in a directory and you need to rename them. The files are named as:

IMG_1232.JPG
SP234324.JPG
webdown.JPG
...

We need to rename them as follows:

salzburg_1.jpg
salzburg_2.jpg
...

Here is how:

j=1
s=salzburg_
for f in *.JPG; do
  mv $f $s$j.jpg
  j=(($j+1))
  echo renaming $f to $s$j.jpg
done

j in is the counter. Line 5 increments j. s is a string. Line 4 uses the mv command renaming each file to the desired combination of text and number. Line 6 prints a message on the command line. Note that in line 3, we only loop through files ending in .JPG.

Substring

The following code show how to get a substring:

#!/bin/bash
a='Freundschaftsbezeugung'
echo $a
echo ${a:0:6} 
echo ${a:6:7}
b=${a:13:9}
echo $b

output

Freundschaftsbezeugung
Freund
schafts
bezeugung
Technologies: