Creating a Shell Script

Creating a Shell Script

with No Comments

This is a brief tutorial on how to create a shell script using a Mac.

 

Create a new blank file in a text editor such as TextEdit.

Put the following as the first line:

#!/bin/bash

Put the desired command(s) in the subsequent line(s), for example:

#!/bin/bash
echo "Hello World!"

Save the file to the Desktop (name it, delete the extension .txt, uncheck “If no extension is provided use ‘.txt’.”).

In the Terminal application, enter the following command:

cd Desktop

To see the permissions of the file, invoke the following in Terminal:

ls -l name_of_script

Note: Replace name_of_script with the name you selected when you saved the file. If your file name has spaces in it, you will need to put quotes around the name.

To make it executable, change the file’s permissions by entering the following command:

chmod 755 name_of_script

Run the file by invoking the following command in the Terminal (or just by double-clicking on the file’s icon on the Desktop):

./name_of_script

 

Alternative method

You can carry out the entire procedure from the command-line:

cd Desktop
nano name_of_script

In the GNU nano text editor that comes up, type in the following:

#!/bin/bash
#Put the command(s) here

Exit and save:

control-x
y
enter

To make it executable, invoke:

chmod 755 name_of_script

Run the file by entering:

./name_of_script

Leave a Reply