Explain the syntax of setting the environment variable in Linux.

For example here:

export PATH=~/folder:${PATH}

Why do I need the part after the colon? What she does? Why can't I just write export PATH=~/folder ?

Does this team differ in any way from this:

export PATH=$PATH:~/folder ?

Why in the first variant the path is indicated before the colon, and the part with the PATH after the colon, and in the second variant the opposite? Will they work in different ways? Why in the first variant braces are placed around PATH, and not in the second? What is the difference?

  • Roughly speaking, just concatenation of strings is used. Most of the questions will disappear if you view the result of each operation echo $VAR . - mkkik

2 answers 2

The PATH variable specifies a list of directories and their order in which the shell of the system will search for an executable file.

For example echo $PATH ,

 /usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin 

Why do I need the part after the colon?

If you write export PATH=~/folder , then the PATH variable will lose the previous value and will be equal only to what is assigned to it. As a result, for example, ls -l will only be accessible via the absolute path /bin/ls -l .

To save the current PATH value, add it after the colon:

 export PATH=~/folder:$PATH 

or vice versa, before the colon:

 export PATH=$PATH:~/folder 

As a result, the ~/folder directory will be added to the PATH variable (at the beginning or at the end) while maintaining the previous paths. If ~/folder goes at the beginning, the search will start from this directory, if at the end, then, accordingly, the executable file in it will be hiccupped last (provided that it will not be found in any of the previous directories).

The curly brackets in this case are not fundamental, i.e. will lead to the same result. And so they are needed for the expanded substitution of variables, various calculations and manipulations with values. All this is detailed with examples in the Advanced Bash-Scripting Guide.

  • Thank you, now everything has become clear)) And there are many recipes in the network, in which it is simply written that you need to drive into the terminal, but it is not written why this is so. - Xander

Why do I need the part after the colon? What she does?

it's just assigning a value to a variable:

 имя_ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ=Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 

Why can't I just write export PATH = ~ / folder?

of course it is possible. but the variable in this case will get a completely different value.

Does this team differ in any way from this:
export PATH=$PATH:~/folder ?

fundamentally nothing. just the variable will get different values.

Why in the first variant the path is indicated before the colon, and the part with the PATH after the colon, and in the second variant the opposite? Will they work in different ways?

there is already a question from a completely different area related not to syntax, but to semantics: after all, the value of the special PATH environment variable has a direct impact on the command search process : if a command is not recognized as a function, an alias, or a built-in shell command, then it is searched in directories listed through the colon in this variable. directories are moved from left to right until an executable file whose name matches the command is found in the next directory, because the order in which directories are mentioned is important: files with the same name (but with different contents) can be found in different directories.

Why in the first variant braces are placed around PATH, and not in the second? What is the difference?

The ways to specify the value of a variable - $имя and ${имя} - are absolutely equivalent. The second method can be considered as one (the simplest) of the so-called recording options. Parameter expansion : ${имя:-слово} , ${имя%слово} , etc.

Also, the second method is convenient (and sometimes necessary) used to eliminate ambiguity. for example, $имя_ will be treated as a value of a variable with the name имя_ , and ${имя}_ will be treated as a value of a variable with the name имя , to which (value) the _ symbol is added on the right.