When it comes to changing the PATH
environment variable (say, in "~/.bashrc"), I've seen some different ways of doing it
Some of them are valid ways of setting $PATH
, but some of them are not valid ways of setting $PATH
; most have the same catch and some differ in their scope;
Speaking of the syntax (in Bash / compatible shells);
PATH=$PATH:/new/path
: is ok, but you'll need to escape spaces in "/new/path", if any;
PATH="$PATH:/new/path"
: is ok, and you won't need to escape spaces in "/new/path", if any;
export PATH = $PATH:/new/path
: is not ok, as you can't have spaces before / after an assignment operator (and you'd need to escape spaces in "/new/path");
export PATH = ${PATH}:/new/path
: same as export PATH = $PATH:/new/path
;
setenv PATH $PATH:/new/path
: setenv
is a csh
built-in; it should be the same as PATH=$PATH:/new/path
;
Single / double quotes both prevent Bash from breaking on whitespaces; single quotes prevent Bash from performing parameter expansions, command substitutions or arithmetic expansions, forcing Bash to interpret the enclosed string literally; double quotes instead don't prevent Bash to perform parameter expansions, command substitutions or arithmetic expansions, and in the second case they are needed in order to allow a parameter expansion on $PATH
;
Braces are required in case the character following a variable is a valid character for a variable name, however :
is not, so in the fourth case they're not really needed;
Speaking of the differences between var=value
, export var=value
and setenv var value
;
var=value
sets the value of $var
in the current shell; forked shells / processes won't inherit the variable nor its value;
export var=value
sets the value of $var
in the current environment; forked shells / processes will inherit the variable and its value;
setenv PATH $PATH:/new/path
: same as export var=value
;
$ foo=bar
$ bash
$ echo $foo
$ exit
exit
$ export foo=bar
$ bash
$ echo $foo
bar
By the way, my Ubuntu 14.04 doesn't have a manual entry for export
, but it has one for setenv
. Why?
If you type man setenv
, you get the output of man 3 setenv
, which is the manual entry of the setenv()
function from the "Linux Programmer's Manual"; as said before, there's no setenv
command in Ubuntu nor built-in in Bash named setenv
, although there's a setenv
built-in in csh
;
export
instead is a Bash built-in, and to get informations about it you'll have to run help export
:
$ help export
export: export [-fn] [name[=value] ...] or export -p
Set export attribute for shell variables.
Marks each NAME for automatic export to the environment of subsequently
executed commands. If VALUE is supplied, assign VALUE before exporting.
Options:
-f refer to shell functions
-n remove the export property from each NAME
-p display a list of all exported variables and functions
An argument of `--' disables further option processing.
Exit Status:
Returns success unless an invalid option is given or NAME is invalid.