Terminal 101: Automate the Terminal with Bash Scripts

Creating the Script

We’ll create the following sample script by first opening TextEdit, and putting the editor into Plain Text mode by selecting Format > Make Plain Text. Next, type in the following script exactly as shown:

echo "Hello, world!"

This line is an echo. This will simply output the contents of the quotation to the terminal screen. In this case, it will output “Hello, world!” to the screen. You can type any terminal command you wish and stack them up in this file. The commands will execute in the order they are typed, one after the other.

Setting the Permissions

Before you can run the script in the terminal, you must set the permissions on the file so that the file can be used as an executable. To do this, open the terminal, and navigate to the location where the file is saved. Next, type in the following command to change the file permissions:

sudo chmod 700 file.sh

Replace "file.sh" with the name of your script.

Running Your Script

To run your script, navigate to its saved location, and type in the following command:

./file.sh

Replace “file.sh” with the name of your saved shell script. This command means to run the script from the current directory.

Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.