Mastering the macOS Command Line: Part 5 — Working with Programs

ElAmir Mansour
9 min readMay 4, 2024

--

Alright we’ve studied the basic command line programs in previous parts , now we will work with programs .

Ready to level up your Mac skills? Buckle up, because we’re about to dive into the world of the macOS command line! If you’ve ever felt daunted by those mysterious-looking Terminal windows, fear not — we’re here to demystify the process and show you just how powerful and user-friendly it can be. In this blog, we’re going to take you on a journey through working with programs on the macOS command line, with a special focus on Zsh. Whether you’re a coding aficionado looking to streamline your workflow or a casual user wanting to explore new frontiers, this guide has something for everyone. So grab your favorite beverage, fire up Terminal, and let’s unleash the full potential of your Mac together!

Interactive Programs: Engaging with Your Mac in Real-Time

Ever wanted to have a direct conversation with your Mac? Well, interactive programs make that possible! These nifty tools allow you to engage with your system in real-time, providing immediate feedback and enabling you to perform various tasks without leaving the command line interface. Let’s take a closer look at how interactive programs work and explore a few examples:

passwd:

passwd

Explanation:

The passwd command is used to change the password for the current user. When you run passwd in the terminal and press enter, your Mac prompts you to enter your current password for verification. Once verified, it then asks you to enter your new password and confirm it by typing it again. This interactive process ensures the security of your account by allowing only authorized users to change passwords.

vim :

vim filename.txt

Explanation:

vim is a powerful text editor that operates directly within the terminal. When you invoke vim followed by the name of a file, such as filename.txt, you enter the vim text editing environment. Here, you can navigate, edit, and save changes to the specified file. vim offers extensive features for text editing, making it a favorite among developers and power users.

and more more more .

Understanding Your PATH: Navigating the Command Line Environment

Have you ever wondered how your command line knows where to find all those handy programs you use every day? It’s all thanks to a little thing called the PATH. Let’s dive deep into how your PATH works and why it’s essential for navigating the command line environment.

What is the PATH?

The PATH is an environment variable in Unix-like operating systems (including macOS) that specifies a set of directories where executable programs are located. When you type a command into the terminal, your operating system searches through these directories, in the order they appear in the PATH, to find the corresponding executable file.

How Does the PATH Work?

When you enter a command in the terminal, such as ls or python, your operating system doesn't magically know where to find the corresponding executable file. Instead, it relies on the PATH to search through a predefined list of directories. The PATH is essentially a roadmap that guides your operating system to the location of executable files.

Example:

Let’s illustrate this with an example. Suppose you want to run the python command in your terminal. When you type python and hit enter, your operating system starts searching for the python executable file.

  1. First, it looks in the directory specified by the PATH variable. This directory is usually /usr/bin.
  2. If it finds the python executable in /usr/bin, it executes the command.
  3. If it doesn’t find the python executable in /usr/bin, it continues searching through the other directories specified in the PATH until it either finds the executable or exhausts the list of directories.

Managing Your PATH:

Now that you understand how the PATH works, you may wonder how to modify it. Fortunately, managing your PATH is relatively straightforward. You can add or remove directories from your PATH to customize the list of locations where your operating system searches for executable files.

To see the current contents of your PATH, enter the following command in your terminal:

echo $PATH

Each valid directory is separated from the next by a colon, as shown in the example output: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin.

Running a Script: Automating Tasks on the Command Line

Scripts are a powerful tool for automating repetitive tasks on the command line. Whether you’re a developer automating build processes or a sysadmin streamlining system maintenance, understanding how to run scripts is essential. Let’s delve into the process of running scripts on the command line and explore some key concepts.

What is a Script?

A script is a file containing a sequence of commands that can be executed by the command interpreter. Scripts are commonly written in scripting languages such as Bash, Python, or Perl and are used to automate tasks ranging from simple file operations to complex system administration tasks.

Running a Script:

To run a script on the command line, you need to follow a few simple steps:

  1. Create or Obtain a Script: First, you need to either write your own script or obtain one from a reliable source. Scripts are usually stored in text files with a specific file extension (e.g., .sh for Bash scripts, .py for Python scripts).
  2. Set Execution Permissions: Before you can run a script, you need to ensure that it has execution permissions. You can do this using the chmod command followed by the appropriate permissions. For example:
chmod +x script.sh

Run the Script: Once the script has execution permissions, you can run it using the appropriate interpreter. For example:

./script.sh

This command executes the script named script.sh located in the current directory.

Example:

Let’s say you have a Bash script named backup.sh that backs up your important files to a specified directory. Here's how you would run it:

  1. Ensure the script has execution permissions:
chmod +x backup.sh

Run the script :

./backup.sh

This command executes the backup.sh script, initiating the backup process as defined within the script.

Running a script on the command line allows you to automate tasks and streamline your workflow. Whether you’re performing routine system maintenance or automating complex data processing tasks, scripts are invaluable tools for boosting productivity and efficiency on the command line. With a solid understanding of how to run scripts, you can harness the power of automation to accomplish more in less time.

Running a Program in the Background: Maximizing Efficiency on the Command Line

In the fast-paced world of command line computing, running programs in the background can significantly enhance your productivity by allowing you to multitask effectively. Whether you’re compiling code, transferring files, or running lengthy computations, learning how to run programs in the background is a valuable skill. Let’s explore how to do just that.

Running a Program in the Background:

To run a program in the background on the command line, you can use a special syntax that allows the program to continue executing while you regain control of the terminal prompt. Here’s how it works:

  1. Basic Syntax: To run a program in the background, append an ampersand (&) to the end of the command. For example:
./my_program &
  1. This command runs my_program in the background, allowing you to continue using the terminal while the program executes.
  2. Viewing Background Jobs: After starting a program in the background, you may want to monitor its progress or manage it later. You can view a list of background jobs using the jobs command. For example:
jobs

This command displays a list of background jobs along with their respective job numbers and statuses.

-Bringing a Program to the Foreground: If you need to interact with a background program or bring it back to the foreground, you can use the fg command followed by the job number. For example:

fg %1

This command brings the background job with job number 1 to the foreground, allowing you to interact with it as if it were running in the foreground from the beginning.

Example:

Let’s say you want to compress a large file using the gzip command but don't want to wait for it to finish before continuing with other tasks. You can run gzip in the background like this:

gzip -9 large_file.txt &

This command compresses large_file.txt using maximum compression (-9) and runs the gzip process in the background, freeing up your terminal for other tasks.

Running programs in the background is a powerful technique for multitasking on the command line. Whether you’re performing resource-intensive tasks or simply looking to maximize efficiency, understanding how to run programs in the background empowers you to make the most of your command line environment. With this skill in your toolkit, you can tackle complex tasks with ease while maintaining control over your terminal session.

To see what programs are currently running on your system, you can use various commands depending on your operating system. Here are some common commands for different platforms:

1. Linux and macOS:

  • ps: The ps command (short for "process status") is used to display information about active processes. By default, it shows processes running in the current terminal session. You can use options like -aux or -ef for a more detailed view of all processes running on the system.
  • Example:
ps aux

top: The top command provides a dynamic real-time view of running processes, CPU usage, and memory usage. It continuously updates the information on your screen, making it useful for monitoring system activity.

Example:

top

htop: Similar to top, htop is an interactive process viewer that provides a more user-friendly and visually appealing interface. It offers features like color-coded process information and mouse support.

Example:

htop

Killing

In macOS with Zsh, you can manage processes using built-in commands. Here’s how you can kill processes:

1. Using kill:

The kill command in macOS Zsh is used to terminate processes. You can specify the process ID (PID) of the target process to kill it.

Example:

kill PID

You can also use the -9 option to forcefully terminate a process if it doesn't respond to the regular termination signal.

Example:

kill -9 PID

Using pkill:

The pkill command allows you to kill processes based on their names or other attributes, making it more convenient than kill in some cases.

Example:

pkill safari

When using these commands, ensure that you’re targeting the correct process to avoid unintended consequences. Additionally, be cautious when using the -9 option, as it forcefully terminates processes without giving them a chance to clean up resources. With these commands, you can effectively manage processes on your macOS system running Zsh.

Editing a Text File :

Editing text files is a common task on the command line, whether you’re writing scripts, configuring system files, or taking notes. Here’s how you can edit text files in macOS with Zsh:

1. Using Text Editors:

macOS comes with several text editors that you can use directly from the command line. Two popular options are nano and vim. Here's how you can use them:

  • Nano: Nano is a simple and user-friendly text editor. You can open a text file for editing by specifying the file name after the nano command.
  • Example:
nano filename.txt
  • This command opens the filename.txt file in the nano editor. You can then make changes to the file, save them, and exit the editor using keyboard shortcuts displayed at the bottom of the nano window.
  • Vim: Vim is a powerful and customizable text editor, but it has a steeper learning curve than nano. You can open a text file for editing by specifying the file name after the vim command.
  • Example:
vim filename.txt
  • This command opens the filename.txt file in the vim editor. Vim has different modes for editing, navigating, and entering commands. To start editing, press i to enter insert mode, make your changes, and then press Esc followed by :wq to save the changes and exit vim.

2. Using Redirects:

You can also use Zsh redirection operators to edit text files directly from the command line without opening a text editor. For example, you can use echo or printf to append text to a file, or cat to display the contents of a file.

  • Appending Text :
echo "New line of text" >> filename.txt
  • Overwriting Text:
echo "New content" > filename.txt
  • Displaying File Contents:
cat filename.text

Using these commands, you can quickly edit text files directly from the command line in macOS with Zsh. Choose the method that best suits your workflow and preferences.

Thanks for reading : D

If you found this blog helpful or have any questions, feel free to reach out to me on social media:

--

--

ElAmir Mansour

🚀 Software Engineer & iOS Developer | Scrum Master 🕹 | Crafting Code & Content | Coffee enthusiast ☕️ | Simplifying Complexity, One Line at a Time 💻