Linux 101: What is the Linux $PATH?

Linux 101: What is the Linux $PATH?

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

linux penguin

Image: Jack Wallen

More about Open Source

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

Actually, the Linux PATH is quite simple, in that it consists of all the directories wherein your user can fun 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’d have something like: /home/jack/hello.sh. 

If you want to run that script, you’d open a terminal window, make sure you’re in your home directory with the command: 

cd ~/ 

Then run the script with the command: 

./hello.sh 

That leading ./ tells bash the command is in the current working directory. 

You couldn’t just issue the command: 

hello.sh. 

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

How do you find which directories are in your path? You issue the command: 

echo $PATH

This would print out every directory in your PATH. 

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

mkdir ~/bin 

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

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

Once you’ve done that, move the hello.sh script to ~/bin and you could then run the script from any directory you choose. That’s all there is to understanding your Linux PATH. You now know what it is, and how to add directories to it, in order to make your command line life a little easier.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Also see

Source of Article