Using Terminal
Last modified on Fri 22 Jul 2022

I don't care if it works on your machine! We are not shipping your machine! - Vidiu Platon

About Terminal

Terminal, also known as command-line interface (CLI) or console, allows you to run commands on a computer without using a graphical user interface. CLI accepts text-based inputs which then converts into functions that the operating system understands. You can use various commands, from simple ones for switching directories to more complex such as running scripts with multiple flags.

How to open Terminal

Use Spotlight search on your Mac to find it.

  1. Press Cmd + Space on your keyboard to open Spotlight
  2. Type in Terminal and hit Enter

Now add it to the Dock to reach it more easily :)

  1. Right-click on the Terminal icon in the Dock
  2. Select Options
  3. Select Keep in Dock

Terminal settings

Terminal appearance

You might want to update the appearance of your Terminal to make it a bit more readable. There are quite a few options to change the color of the background, size and color of the text, cursor, and so on.

  1. Open Terminal
  2. Click Terminal in the toolbar
  3. Select Preferences
  4. Select the Profiles tab
  5. Select the Text tab

Change Terminal behavior

There are also various options to edit the Terminal behaviour, spread across multiple tabs: Window, Tab, Shell, Keyboard, Advanced.

For example, under the Shell tab you can specify the command that is run on startup:

  1. Select the Shell tab under Profiles
  2. Select the Run command checkbox
  3. In the input field, type in the command you wish to run, for example:
    • cd /Users to open the Terminal positioned to the Users directory
    • echo $$ to run the echo command right after the Terminal opens

Another interesting option is to close the Terminal window when the shell exits. This can be useful when you run shell commands from your project and don't want to leave the Terminal open.

  1. Select the Shell tab under Profiles
  2. From the dropdown in When the shell exits select Close the window option
    • if you run the exit command, the Terminal window will close

Terminal alternatives

The Terminal is not the only app for using the command line on the Mac. There are various alternatives such as iTerm2, Hyper, Upterm and Alacritty, to name a few. Each of these comes with some differences when compared to the Terminal, often adding additional features.

Shell

A shell is a computer program that basically allows you to control your computer. You can use it to control other programs, interact with the system, or simply automate repetitive tasks. The commands can be entered directly, or read from a file called the shell script.

To access a shell you need an app that runs it, such as Terminal/iTerm2 on Mac or Command Prompt/GitBash on Windows.

There are various shell programs to choose from, like sh, bash, ksh, tcsh and zsh.

Command-line arguments

When reading about command-line arguments, you might come across terms like flag, switch, option, argument, parameter, and so on, which are often used interchangeably.

Arguments tell the process or function to perform a different operation depending on its value. Arguments are often used in command-line programs, and they are basically options that change the behaviour of a program.

There are a couple of types of arguments:

Arguments can start with a single dash (-) or a double dash (--).

A flag argument usually refers to a boolean argument that by including it, it changes the behaviour of a program. If you include a flag argument its boolean value is set to true, if not, it is set to false. For example,

ls - lists all visible files and folders in the current folder, but if we add the flag -a, making it

ls -a - it lists all visible and invisible files and folders.

In the following example command, using the --report flag we tell the process to include/activate the report option. With the --report flag included a report is generated, and without the flag it is not.

python test.py --report

Arguments that start with a double dash (--) often take an argument themselves. For example, in a Python script we might have the env flag that activates a different environment depending on the value we specify when including the --env argument:

python test.py --env=test_environment

python test.py --env=staging_environment

For more details on arguments, read this blog post.

List of useful commands

The following list of commands works in the Terminal but might not work in other similar apps.

Directory / Folder navigation

Command Description
cd path/to/directory Position to a directory
cd .. Go back to the directory above the current one
cd Go back to the root (Home) directory

Permissions

There are three basic file system permissions (also known as modes) on Unix and Unix-like systems:

You can use the chmod (short for change mode) command to change the access permissions for a system file or folder.

Command Description
man chmod Open chmod manual in the Terminal
chmod +x /path/to/file Add the executable permission for the file
chmod +rwx /path/to/file Add the permissions for the file
chmod -rwx /path/to/file Remove the permissions for the file

Echo

Command Description
echo $$ Get process ID (PID) of the current terminal/tab

Grep

grep is a command used for searching plain text.

It can be used on its own or in combination with other commands, separated by the vertical pipe symbol (|).

Command Description
grep "some text" file_name.txt Searches for "some text" in the specified file
ps -ax | grep Appium Display processes that contain Appium

For more on the grep command, see this article.

List files and folders

Command Description
ls List all visible files and folders in the current folder
ls -a List all visible and invisible files and folders in the current folder
ls -l List all visible files and folders in the current folder with permissions

List of open files

Command Description
lsof List all open files and processes
lsof -i 6 List open files that use IPv6 protocol
lsof -p 66460 List open files and processes with PID 66460

Open file / directory

Command Description
open directory_name Open a directory
open directory_name/file_name.txt Open a file
open ~/.zshrc Open zsh resource file

Process status

Command Description
ps -ax Displays your and other users' processes

Print working directory

Command Description
pwd Display the path to the current/working directory

Close / Quit terminal

Command Description
exit Quit Terminal
python some_script.py; exit Quit Terminal right after running a command

System info

Command Description
id -un Get system name
hostname Get hostname
system_profiler Display system information (long version)
system_profiler -detailLevel mini Display system information (short version)

Networking

Command Description
arp -a View a list of all active devices on a local network

Additional resources