Technology

#What Is the Unix PATH, and How Do You Add Programs To it? – CloudSavvy IT

“#What Is the Unix PATH, and How Do You Add Programs To it? – CloudSavvy IT”

The Unix PATH is an environment variable that tells the shell which directories it should look for when you issue commands. You can think of it like a list of Application or Program Files folders.

What Does The PATH Do?

If you wanted to run a binary in your current folder, you’d have to provide a relative path, like so:

./command [args]

But, if you move that binary to a folder like /usr/local/bin/, you instead simply type the command name to run it:

command [args]

This is because /usr/local/bin/ is on the PATH. The shell knows to look in that folder if you don’t specify a location. Many command line utilities are installed here, and in similar folders. You can view which folders are on your PATH by echoing the variable itself, using the $ prefix:

echo $PATH

By default though, the PATH variable is a list of directory names split by colons. It’s not very readable:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

But, with a bit of regex, you can clean it up to print out a list:

echo $PATH | sed $'s/:/\n/g'

These directories all serve different purposes:

  • Folders ending in bin are general purpose, folders ending in sbin are intended for system administration commands, things the system runs but you probably won’t interact with much. Though there are
  • /bin and /sbin are intended for programs that the system needs before /usr is available. This is where key programs like sh and ls are stored.
  • /usr/bin and /usr/sbin are for normal user programs, usually built in to the OS.
  • /usr/local/bin and /usr/local/sbin are for programs you’ve downloaded or compiled yourself. If you have a binary you downloaded, and don’t know where to put it, chuck it in /usr/local/bin.

/usr/local/sbin may not even exist on your system, as it’s fairly contradictory in nature. It isn’t present on macOS, but contains a few programs on Ubuntu.

You can view which folders different commands are located in with the which command:

which ls

Which will print out the full path leading to the given command.

How To Add Folders To The PATH

Adding a folder to the PATH is simple, you just have to set the variable. But you can’t overwrite it completely, so you instead add a folder to the front of the PATH:

PATH=~/folder:$PATH

You can also tack the folder onto the back of the PATH:

PATH=$PATH:~/folder

These have different uses. Folders close to the front will be checked first, so if there are duplicate commands (for example, two versions of Python, both called “python“) adding the folder to the front will ensure the folder you added manually will be checked first. Most of the time, this is probably what you want.

Usually, you’ll define this in your shell’s profile, which will set the variable whenever you open a new terminal. If you’re using bash, this is either .bash_profile or .bashrc. If you’re using a shell like zsh, this will be a different file (.zshrc in this case). If there’s already a definition, you can simply add your folder to the front of the list, separated by a colon.

Consider Using Symlinks

Rather than adding a folder directly, you can instead use a symlink to specific commands, and place the symlink in/usr/local/bin. For example, Python installs to its own folder, but rather than adding that folder to the PATH, it uses a symlink. You can view where symlinks lead with ls -la:

ls -la $(which python)

And you can create symlinks with ln:

ln -s command /usr/local/bin/command

The PATH Can Change

Since the PATH variable is usually defined in .bashrc, your PATH may be different when running in a different environment. This is particularly relevant if you’re running inside a bash script, or if you’re running a command via cron. If you’re having “command not found” issues, you’ll want to double check that the PATH is set correctly in the environment you’re running in.

If you liked the article, do not forget to share it with your friends. Follow us on Google News too, click on the star and choose us from your favorites.

For forums sites go to Forum.BuradaBiliyorum.Com

If you want to read more like this article, you can visit our Technology category.

Source

Related Articles

Leave a Reply

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

Back to top button
Close

Please allow ads on our site

Please consider supporting us by disabling your ad blocker!