How to Run a Script in Linux Command Line

A shell script is a collection of commands that provide a useful function. The script can be run from the terminal manually or automatically on a schedule using Cron.

 

In this guide, we will learn at how to create a simple shell script and then how to run it.

 

Writing a Shell Script

Before we go through how to run a script let's create a simple shell script. To do this we will need to use a terminal-based editor. Ideally, you would do this with an editor that offers advanced features such as Vim, though to keep things simple we will use nano, which only requires very basic knowledge to use.

 

The first step is to open a terminal and locate to a directory where you would like to create the script using the cd command. Now let's open the nano with the intention to save-out a file called shellscript.sh.

 

nano shellscript.sh

You can call the file whatever you wish so long as it doesn't already exist in your current directory, the important part is to have the .sh shell script extension.

 

Once nano is open let's add the code a for simple program. The program is simply going to to say hi to the user who ran the script and print todays date.

 

#!/bin/bash
echo "Hello $USER."
echo "Today is $(date)"

 

Next, we need to save the file and exit nano. To do this press the key combination CTRL + X then press Y when the confirmation dialogue appears, then press ENTER again to confirm the file name to save.

 

Save shell script from Nano

 

Apply Execute Permissions to Script File

Our newly created file will probably not have execute permissions, meaning you will not be able to run the script. To change this we can add the x (execute) permission flag to the file using chmod.

 

chmod +x shellscript.sh

 

To check the file has the correct permissions run ls in the long list format mode.

 

ls -l shellscript.sh
-rwxr-xr-x  1 johnhoward  staff  56 30 Jun 21:24 shellscript.sh

 

How to Run the Script Using the Command Line

To run the script we simply put ./ (dot followed by a forward slash) directly before the script file name.

 

./shellscript.sh
Hello johnhoward.
Today is Tue 30 Jun 2020 21:50:48 BST

You will see that the output of the script is immediately returned to you.

 

How to Run a Script in Cron

Cron is a job scheduler built into Linux and other Unix-like operating systems. Scheduling scripts to run at certain intervals can be very useful. Let's take a look at how we can set a shell script to run on a schedule using Cron.

 

The place where jobs are set for Cron is in a file called crontab. To edit this file and add a task type in crontab followed by an -e flag; the -e flag means edit.

crontab -e

You should see an empty file appear.

 

Next type in five space-separated * (asterisks) followed by an absolute path to your shell script.

* * * * *  /Users/johnhoward/shellscript.sh

The stars are a representation of intervals of time. From left to right they are minute, hour, day, month, day (week).

 

To turn off the script running every minute so it will only run once an hour we can change the first * (asterisk) to 0.

 

0 * * * *  /Users/johnhoward/shellscript.sh

 

To save and exit the crontab:

  1. press: ESC
  2. type: :wq
  3. press: ENTER

 

I am going to be writing a full guide on how to use Cron and how to test that scripts are running in an article soon.

 

Conclusion

You now know how to create a basic shell script, run it directly from the command-line and schedule it to run VIA Cron. I will be creating a detailed article about Cron shortly covering real-world usage of scripts.

 

terminal command line script