Georgi Georgiev
Georgi Georgiev

Reputation: 1623

about PATH variable UNIX

I see in examples of .profile file in UNIX systems, that after giving a value to the PATH variable e.g. PATH=$PATH:. and then it is exported export PATH.

My question are 2:

  1. Why will happen if I don't export PATH after giving it a value?
  2. PATH=$PATH:. means that the current directory is added to PATH variable. But is this done only 1 time? I mean will it just add my home directory, or every time I change the directory it will add it to PATH?

Upvotes: 2

Views: 178

Answers (3)

rodrigo
rodrigo

Reputation: 98496

  1. Probably nothing. Once a variable is exported, it will keep being exported even if reassigned. And it is expected that PATH is already exported when .profile is sourced. But, just in case, it is exported anyway. If the variable weren't exported, it would still work in the current shell, but it wouldn't be inherited by any child process.
  2. . means the current directory, whatever it is, but it is never expanded to the real name of the directory (for that use pwd enclosed in back-quotes). If you change the directory, the current directory will be in the path. That is similar to the behavior of other non UNIX-like operating systems (Windows and DOS), but it is generally considered a security risk. To minimize it, at least put it at the end of the PATH, as in your example.

Upvotes: 3

jxh
jxh

Reputation: 70492

If you don't export PATH, then when you start another program (or sub-shell), that program will not see the same value for $PATH that you have. Exporting it means the value that you see is also seen by the child processes that your shell spawns.

Adding . to the PATH does not expand into your current directory name when you added it. It stays a .. So whatever current directory you happen to be in will be the directory that gets searched for executables when you try to run a program.

Upvotes: 4

Lars Kotthoff
Lars Kotthoff

Reputation: 109272

If you don't export a variable, it will remain local to the script that's running, i.e. .profile in this case. That means that if you reference $PATH anywhere else, it will be empty.

The variable is only set when the statement is executed, i.e. when .profile is run. Usually, this will only happen once (when you start the shell) and not when you change directories.

Upvotes: 0

Related Questions