Chenglu
Chenglu

Reputation: 884

What does the colon do in PATH

I am new to bash, and I saw people often add : after a directory when modifying PATH. After searching for a while, I did not find an answer for that, or I believe I did not search it correctly. So I hope I could get an answer here.

Example:

/Users/chengluli/anaconda/bin:/Users/chengluli/.rbenv/shims:/

What does the : after bin and shims do?

Upvotes: 39

Views: 39277

Answers (4)

gzh
gzh

Reputation: 3596

Th quotation from output of man bash command

PATH
The search path for commands. It is a colon-separated list of directories in which the shell looks for commands (see COMMAND EXECUTION below). A zero-length (null) directory name in the value of PATH indicates the current directory. A null directory name may appear as two adjacent colons, or as an initial or trailing colon. The default path is sys‐tem-dependent, and is set by the administrator who installs bash. A common value is ``/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin''.

If you have questions about bash script or environment variable, please use man bash firstly.

Upvotes: -1

cdarke
cdarke

Reputation: 44354

As others have said, the : is a separator (Windows uses a semi-colon ;). But you are probably thinking of a trailing colon : at the end of the PATH variable. For example:

/Users/chengluli/anaconda/bin:/Users/chengluli/.rbenv/shims:

From the bash man pages:

A zero-length (null) directory name in the value of PATH indicates the current directory. A null directory name may appear as two adjacent colons, or as an initial or trailing colon.

Putting the current directory in the PATH is generally considered a security risk and a bad idea. It is particularly dangerous when using the root user.

By the way, bash only uses $PATH on the first call of an external program, after that it uses a hash table. See man bash and the hash command

Upvotes: 16

slashfoo
slashfoo

Reputation: 606

: is the separator. The PATH variable is itself a list of folders that are "walked" through when you run a command.

In this case, the folders on your PATH are:

  • /Users/chengluli/anaconda/bin
  • /Users/chengluli/.rbenv/shims
  • /

Upvotes: 43

Huihoo
Huihoo

Reputation: 133

If you run ls -l 123 at the command line, you are telling bash to find the command called ls in the filesystem. However, ls is just a file name, bash needs the absolute path of ls in the filesystem. So bash searches for a file called ls in a list of default directories, one by one in order.

A list of default directories is stored in the PATH variable, separated by :.

Upvotes: 2

Related Questions