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:
Then run the script with the command:
That leading ./ tells bash the command is in the current working directory.
You couldn’t just issue the command:
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:
This would print out every directory in your PATH.
Now, let’s say you’ve created the directory bin in home with the command:
You can add that directory to your PATH with the command:
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.
Subscribe to the Developer Insider Newsletter
From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays