
The Terminal app on macOS is a powerful interface for controlling your Mac using the command-line environment. Whether you’re a developer, IT admin, or just curious about the command line, Terminal offers robust features for executing commands, managing files, and automating tasks. Here’s a comprehensive guide to using Terminal effectively.
Using the Command-Line Environment
The command line can be used in two ways:
- Interactively: Type a command, wait for the result, and continue.
- Through Scripts: Write scripts that execute multiple commands without direct interaction.
Execute Commands in the Shell
- Open Terminal: Launch the Terminal app on your Mac.
- Enter a Command: Type the full path to the executable, followed by required arguments, and press Return.
- Omitting Paths: If the command is in a folder listed in the PATH environment variable, you can omit the full path.
- Troubleshooting: If you encounter command not found, double-check the command spelling.
Terminate Commands
To stop a running command:
- Click the Terminal window where the command is running.
- Press Control-C to terminate it.
Repeating Commands
Terminal remembers commands entered in a session. To reuse a previous command:
- Press the Up Arrow key to navigate through your command history.
- Press Return to execute the selected command.
Specify Files and Folders
Most commands operate on files and folders identified by their paths:
- Use/ to separate folder names.
- Shortcuts:
. : Current folder.
.. : Parent folder.
~ (Tilde) : Home folder of the logged-in user.
Example:
~/Documents/Home.txt
Special Cases:
- For paths with spaces, use quotes or escape characters:
“Home Folder” or Home\ Folder
Redirect Input and Output
Redirect data to and from commands for advanced control:
- Use > to write output to a file:
% ls > redirectedoutput.txt
- Use >> to append the output to a file:
% ls >> appendoutput.txt
- Use < (Input Redirection)to read input from a file:
% sort < sendinputtocommand.txt
- Use | (Pipe) to connect commands:
% man zsh | grep commands
- Use !! to re-run the previous commands. You can use with other commands like sudo !! to run the previous command with sudo privileges:
% sudo !!
Command Execution Operators
- Use & (Background Execution) to execute command in the background, allowing the shell to continue executing other commands without waiting for the first command to finish. The shell runs the command in the background and returns control to the user or continues executing subsequent commands without waiting for the background process to complete.
% command &
- Use && (Logical AND) to execute two or more commands sequentially, ensuring that the subsequent commands run only if the preceding one succeeds (i.e., exits with a status code of 0). The shell runs command1 first. If command1 succeeds (exit status 0), the shell runs command2 / the shell skips command2. If any command fails (non-zero exit status), the subsequent commands will not be executed.
% command1 && command2
- Use || (Logical OR) to execute a second command only if the first command fails (exits with a non-zero status). command1 is executed first. command1 fails (non-zero exit status), then the shell runs command2 / the shell skips command2. If command1 succeeds (exit status 0), command2 is skipped.
% command1 || command2
- Use ; (Sequential Execution) to execute multiple commands sequentially, regardless of whether the previous command succeeds or fails. command1 is executed first. Once command1 finishes (regardless of its success or failure), the shell runs command2 / the shell skips command2. This process continues with the remaining commands.
% command1 ; command2 ; command3
- $? is a special variable that holds the exit status of the last executed command. An exit status of 0 means success. A non-zero exit status means failure (usually 1, but commands vary). Use $? for debugging, conditional logic, and error handling in scripts.
% echo $?
Tips: Use these commands to execute multiple actions in a single line. For example, With File and Processes payload of a Jamf policy.
Single-line comment
Use # at the beginning of the line.
# This is a single-line comment
echo “Hello, World!”
Multi-line comment:
Use : ‘ ‘. Everything inside the quotes will be ignored.
: ‘
This is a multi-line comment.
It spans multiple lines.
‘
echo “Hello, World!”
Drag Items into Terminal
Save time by dragging files or text snippets into Terminal:
- Drag a file or folder to insert its absolute path.
- Drag an executable file and press Return to run it.
- Drag text clippings to include their contents.
Explore More
For additional commands and usage tips, check out the Command Line Primer or refer to the zsh man page for details on advanced features.
Follow TexArxs more tips on mastering macOS Command Lines.