This series is an excerpt from the first chapter of my upcoming book “Scripting macOS” which will teach you to use and create shell scripts on macOS.
- Part 1: First Script
- Part 2: The Script File
- Part 3: The Code
- Part 4: Running the Script
- Part 5: Lists of Commands
- Part 6: Turning it off and on again
I will publish one part every week over the summer. Enjoy!
When learning programming languages it is tradition that the first program you write displays ‘Hello, World!’
Printing these words to the screen is usually a really simple task. In most programming languages it requires just a single line. Nevertheless, creating a simple program or script like this will teach you a lot about all the other tasks you need to create and run a script.
Before you start, you should create a folder where you store all the scripts you will create. A
Documents subfolder is a good location. I have a
Projects folder in my home directory where I create subfolders for all my scripting and programming projects. You may already have some structure like this that makes sense for you.
You can create this in Finder and the navigate there in Terminal. But, just to practice working in Terminal, you can create your script project folder in the command line:
> cd ~/Documents > mkdir ScriptingMacOS > cd ScriptingMacOS
Where- and however you create this folder, we will refer to it as your script project folder from now on. When you interact with the scripts from the command line, always remember to change the working directory there.
Note: When you know how to use a version control system to manage code, such as git, you can set up or initialize this project folder as repository now. Shell scripts are well suited to be managed with version control systems and I would recommend to use it when building scripts. But explaining the details of git as well as teaching shell scripting would be overwhelming and beyond the scope of this series.
Create your first script
Create a new text file in your favored text editor.
Note: If you do not have a favorite text editor yet, I recommend BBEdit. You can use it for free or pay the license fee to unlock the full feature set. After installing BBEdit, be sure to run ‘Install Command Line Tools…’ from the BBEdit menu.
I will be using the bbedit command line tools in my examples, but other text editors have similar commands.
When you are using a text editor with a command line tool, you can create a new empty file from the command line and open it in the text editor like this:
> bbedit hello.sh
Enter the following text into the text document:
#!/bin/zsh # Greetings echo "Hello, World!"
Tip: You can copy and paste the code from this post into the text editor. In general, I approve of this, since it speeds up the process and avoids typing errors. But in the beginning, I would recommend typing out the commands and modifying the script manually. This will engage your brain and memory differently and help you memorize some of the standard steps and commands.
Save the text file you created to a file named
hello.sh in the script project folder.
In Terminal, ensure that the working directory is your script project folder. Then enter the following:
> chmod +x hello.sh
This will make your script file executable. You are telling the system this file has code that can be run as a program rather than just data. We will look at what this means in detail later.
Now you can run or execute your script:
> ./hello.sh Hello, World!
If you get a different result, please verify that you typed the script exactly as given above. Pay extra attention to spaces, quotes and other special characters. Then save the text file and try running the script again:
> ./hello.sh Hello, World!
Even for a script as simple as this, there is a lot that happened here. We will look at the pieces and steps in detail in the next part.