Mastering the macOS Command Line: Part 6— Modifying PATH
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:
- Open your terminal.
- Check the current value of PATH by typing:
echo $PATH
- Let’s assume the output includes directories like
/usr/bin
,/bin
, etc., but not~/bin
. - To add
~/bin
to your PATH temporarily for the current session, you can run:
export PATH=$PATH:~/bin
- This command appends
~/bin
to the existing PATH. - Verify that
~/bin
is now included in PATH by typing:
echo $PATH
- You should see
~/bin
listed at the end. - 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:
- Open your terminal.
- To create an alias called
lla
forls -la
, run:
alias lla='ls -la'
- Now, whenever you type
lla
in your terminal, it will be equivalent to typingls -la
. - 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:
- YouTube: ElAmir’s YouTube Channel
- Facebook: ElAmir’s Facebook Page
- LinkedIn: Connect with ElAmir on LinkedIn
- Twitter: Follow ElAmir on Twitter
- Udemy: ElAmir’s Udemy Profile