My First Script

This is not my first script but I was asked by a friend to start him off in the right direction so after writing this out I thought I should at least post it!

Ubuntu, for some reason unknown to me uses the Dash shell which causes problem with some scripts written in the standard Bash shell. To make things easier from the start it is better to swap over to do this open a Terminal [Applications], [Accessories], [Terminal] and type or copy & paste the following;

echo $SHELL

and press [Enter], you should get a response like this;


which is your current Shell, the [echo] means print the following to the screen. Now lets change it to the Bash shell, type the following in the [Terminal]

sudo rm -f /bin/sh

and press [Enter], you will then be asked to enter your password as the sudo option means you are working as the root [super user] and this requires a password to make it secure. now type the following;

sudo ln -s /bin/bash /bin/sh

the first command removes the symlink [symbolic link, which Windows users will know as a shortcut] which links Dash to the sh command and the second adds a link from the bash shell to the sh command. Now type this again;

echo $SHELL

your should now get;


Now lets set-up a folder in your home directory to store your scripts in and allow the system to locate them by adding the new directory to your path. Create a new folder by typing;

mkdir bin

as the [Terminal] starts up in you [home] directory the bin directory will be created there. you can list the contents of your home by typing;


and [Enter] which will list all the directories [in blue] & files [in black]. Now lets add the bin directory to your path, this is where the system looks for files you ask it to launch. To see your current path, type the following.

echo $PATH

which will give you something like;



and then repeating the command;

echo $PATH

will now give you;


Any executable scripts you place in your bin directory can run by typing their name in a [Terminal] as we will see later. We will now create our first script using the Touch command.

touch ~/bin/

the ~/ is short for the path to your home directory i.e. /home/martin/ and the rest adds the file called in the bin directory. We need to add some commands to the file so open it in gedit by typing;

gedit /home/martin/bin/

Then copy and paste the following into the file, save it and close gedit, the first line is know as the “shebang” and point the system at the shell you are using, the lines starting ## are comments and will not be run.

## created by Martin May 2009 ##
## say hello ##
## we give the variable today a value which represents ##
## day, date & time and prints it later by adding a $ to it ##
today=`date +%a%t%d%t%B%t%Y%t`
## ask your name and read it into a variable called name ##
echo -n “Enter your name: ”
read name
## clear the screen ##
## we now give the variable time the current system time ##
time=`date +%T`
## now lets print it all out to the screen ##
echo “Hello World!”
echo “Welcome ” $name
echo “Today is “$today
echo “and your logon time was : “$time

We can now try and run the script by typing it’s name in the [Terminal];


which will result in;

bash: /home/martin/bin/ Permission denied

This is because the script is not yet executable, so type the following;

chmod +x ~/bin/

and run the script again with;

It should then ask for your name and display the result with a welcome message including the time as follows;

Hello World!

Welcome Martin

Today is Tue 15 May 2009

and your logon time was : 22:33:24

Once you have this basic script running, you can start making changes and playing with the output.


Tags: , , , ,

2 Responses to “My First Script”

  1. Backup using rsync, SSH & cron « Martin’s Ubuntu Blog Says:

    […] and saved with the Permissions set to “allow executing as a program”, check out my post here for help with that, the content of the script […]

  2. Gena Says:

    Great info. Lucky me I ran across your site by accident (stumbleupon).

    I have saved it for later!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: