Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!
Repeating commands in the terminal is easy by setting aliases, but if you need to repeat multiple commands, one after the other, then you’ll need Bash (or Shell) scripting. With a shell script, you can stack up commands, store them in a file, and run the stack of commands at will. Continue reading to learn how shell scripts can be used.
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:
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.