/ Ubuntu

Writing a Simple Ubuntu Shell Script

If you have ever done a lot of repetitive tasks on a Ubuntu Linux machine, I’m sure you’ve wondered if there’s a better way of doing things. For example, say I need to run a dozen or more command line commands. I could enter them in one by one or I can put them all in a single file and run it as a script.

When you run it all as a script, you save a lot of time. Today, I am going to show you how to make the most simple Ubuntu script you can find. It doesn’t use any variables, it just helps you run several commands at once. for example, we are going to create a simple script to run Firefox.

First, just you need to create the script file. You can use a GUI editor or command line editor. Your choice. When originally written, I preferred gedit. Now I prefer nano. For this just open a terminal and type:
GUI Editor: gedit samplescript.sh
Command Line Editor: nano samplescript.sh

This will open gedit(or nano) with a blank file named samplescript.sh. In the file, type the following:

#!/bin/sh
echo “welcome”
firefox

Save this file. The first line identifies it as a shell script, the second line causes the script to print welcome on your screen when it’s run. After the echo, you can put in any commandline line you want to on the next line and it will run. For this example, we put the command firefox and when you run the file, it will open Firefox.

Next, run this command to make the file executable:
Unsecure way: chmod 777 samplescript.sh
Better and More secure way: chmod +x filename.sh

Now, to run the script, type this:
./samplescript.sh

See how easy that was? Hopefully this can save you some time now.

Writing a Simple Ubuntu Shell Script
Share this