Mastering the macOS Command Line: Part 6— Modifying PATH

ElAmir Mansour
2 min readMay 5, 2024

--

PATH is an environment variable that lists directories where your shell looks for executable files when you type a command. By default, it includes directories like /usr/bin, /bin, /usr/sbin, etc.

Scenario: You have a custom script located in ~/bin directory that you want to run from anywhere in the terminal without specifying its full path.

Steps:

  1. Open your terminal.
  2. Check the current value of PATH by typing:
echo $PATH
  1. Let’s assume the output includes directories like /usr/bin, /bin, etc., but not ~/bin.
  2. To add ~/bin to your PATH temporarily for the current session, you can run:
export PATH=$PATH:~/bin
  1. This command appends ~/bin to the existing PATH.
  2. Verify that ~/bin is now included in PATH by typing:
echo $PATH
  1. You should see ~/bin listed at the end.
  2. To make this change permanent, you can add the export command to your ~/.zshrc file:
echo 'export PATH=$PATH:~/bin' >> ~/.zshrc

This ensures that ~/bin is added to your PATH every time you open a new terminal session.

Explanation:

By adding ~/bin to your PATH, you enable your shell to find executable files located in that directory without needing to specify their full paths. This makes it more convenient to run custom scripts or executables from anywhere in your terminal.

2. Creating Aliases:

Understanding Aliases: Aliases are shortcuts or alternate names for commands or command sequences. They allow you to define your own custom commands or abbreviations for frequently used commands.

Scenario: You frequently use ls -la to list all files in a directory, including hidden ones, and you want to create a shorter alias for this command.

Steps:

  1. Open your terminal.
  2. To create an alias called lla for ls -la, run:
alias lla='ls -la'
  1. Now, whenever you type lla in your terminal, it will be equivalent to typing ls -la.
  2. To make this alias permanent, add the alias command to your ~/.zshrc file:
echo "alias lla='ls -la'" >> ~/.zshrc

This ensures that the alias is available in every new terminal session.

Explanation: Creating aliases allows you to customize your command line experience by defining shortcuts for frequently used commands or command sequences. This can save you time and typing effort, especially for long or complex commands.

In this example, we’ve explored how to modify the PATH variable to include custom directories and create aliases for commonly used commands. These customization techniques can help streamline your workflow and make your command line experience more efficient and personalized.

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 💻