Linux 101: what is the Linux $ PATH?

Jack Wallen offers another Linux 101 tip, this time explaining and demonstrating Linux $ PATH.

Image: Jack Wallen

If you are new to Linux, you may have heard of something called PATH. Or, you’ve seen it written as: $ PATH. What is this mysterious convention? Is it less followed or something dark and dangerous that you might want to avoid?

In fact, the Linux PATH is pretty straightforward, in that it consists of all the directories in which your user can use commands from anywhere. Let me explain.

Let’s say you write a bash script to print out “Hello, TechRepublic!” and named it hello.sh. You give it executable permissions and leave it in your home directory.

SEE: 5 Linux Server Distributions You Should Be Using (TechRepublic Premium)

You would have something like: /home/jack/hello.sh.

If you want to run this script you need to open a terminal window, make sure you are in your home directory with the command:

cd ~/ 

Then run the script with the command:

./hello.sh 

This leader ./ tells bash that the command is in the current working directory.

You couldn’t just issue the command:

hello.sh. 

You could if this script was in a directory found in your PATH. In other words, the directories in your path are the ones that allow you to run commands from any directory in the file system hierarchy.

How do you find which directories are in your way? You launch the command:

echo $PATH

This would print all the directories on your PATH.

Now let’s say you created the bin directory in home with the command:

mkdir ~/bin 

You can add this directory to your PATH with the command:

export PATH="$HOME/bin:$PATH" 

Once done, move the hello.sh script to ~ / bin and then you can run the script from any directory you want. That’s all it takes to understand your Linux PATH. Now you know what it is and how to add directories to it, to make your command line life a bit easier.

Subscribe to TechRepublic How to Make Technology Work on YouTube for all the latest technical advice for business professionals from Jack Wallen.

Also look


Source link

About Adam Gray

Adam Gray

Check Also

The Minimum Guide to Local SEO for a Small Business

The thing you learn about owning a small business is that there is always something …

Leave a Reply

Your email address will not be published. Required fields are marked *