12

Can anyone explain what does this command mean?

PATH="${GZIP_BINDIR-'/bin'}:$PATH"; export PATH
  • Put shortly, you're adding a directory to a list of folders, and if you have executable files in that folder, you can run them anywhere – Sergiy Kolodyazhnyy May 27 '15 at 22:17
  • 1
    BTW, for generic question visit unix.stackexchange.com – Pandya May 29 '15 at 5:33
18

${GZIP_BINDIR-'/bin'} is a bash parameter expansion pattern. From man bash:

${parameter:-word}
              Use Default Values.  If parameter is unset or null, 
the expansion of word is substituted.  Otherwise, the  value  of parameter
is substituted.

Immediately prior to this portion man page has (thanks to @jwodder for notifying):

Omitting the colon results in a test only for a parameter that is unset.

As the manpage says, it will set the default value of the variable GZIP_BINDIR. If GZIP_BINDIR has a value previously set (other than null as you are not using :) then that value will be used, otherwise /bin will be used as the value of the variable GZIP_BINDIR.

On the other hand if you want to test for null values too, use this pattern (include :):

${GZIP_BINDIR:-'/bin'}

After that the $PATH will be expanded to the values it was holding before. So, if PATH previously had:

/usr/sbin:/usr/bin

Now your PATH will be (considering GZIP_BINDIR is not set) :

/bin:/usr/sbin:/usr/bin

export PATH will make this PATH available to all the child processes.

  • "I think you have missed a : in ${GZIP_BINDIR-'/bin'}" Not necessarily. ${parameter-word} is valid too, though doesn't seem to be in the man page. – nyuszika7h May 27 '15 at 15:09
  • @nyuszika7h Yeah..i didn't know it before..strange why its not included in the manpage.. – heemayl May 27 '15 at 15:14
  • In the bash manpage on my computer, the paragraph right above the section on :- says: "When not performing substring expansion, using the forms documented below (e.g., :-), bash tests for a parameter that is unset or null. Omitting the colon results in a test only for a parameter that is unset." – jwodder May 27 '15 at 21:57
  • As a side-note, you can combine the setting of PATH and the export into a single command as follows: export PATH="${GZIP_BINDIR-'/bin'}:$PATH" – Paddy Landau Jun 2 '15 at 14:56
7

Is there really no colon before the hyphen inside the parameter expansion ${GZIP_BINDIR-'/bin'}?

This kind of construct is almost always written with a colon, as in ${GZIP_BINDIR:-'/bin'} which means, "if $GZIP_BINDIR is NULL or unset, set it to '/bin'"

However, the syntax you supplied is valid, but rare. ${GZIP_BINDIR-'/bin'} (no colon) means, "if $GZIP_BINDIR is unset, set it to '/bin'". If it is set (has been declared) but nothing assigned to it (e.g. it is NULL) nothing will be inserted.

There may be a bug here though as the colon following the variable construct (...}:$PATH...) will still be inserted if $GZIP_BINDIR is set but NULL. Since this would add a colon at the beginning of the $PATH, it will cause the current directory to be searched first.

That is considered by most to be a security problem since it makes it easier to cause a malicious program with the same name as standard commands to be run just by putting it in the current directory.

To leave $PATH unchanged if $GZIP_BINDIR is set but NULL (empty), the correct syntax would be ${GZIP_BINDIR-'/bin:'}$PATH; export PATH

To leave $PATH unchanged if $GZIP_BINDIR is either unset or is set but NULL, the correct syntax would be ${GZIP_BINDIR:-'/bin:'}$PATH; export PATH

2

This command update the PATH variable.

The PATH is an environment variable specifying a set of directories where executable programs are located.

PATH="${GZIP_BINDIR-'/bin'}:$PATH";

This add the value of another variable GZIP_BINDIR if exit to the previous PATH else use /bin instead and add it to PATH

export PATH

export is used to set the PATH available for all child processes and subshells Read this for more information about

You must note that this is not static and you'll loose it once you exit your shell.

In order to make this PATH edit permanent you have to append these lines to your .bashrc

gedit .bashrc

and add the line

export PATH="${GZIP_BINDIR-'/bin'}:$PATH"

then run the command

source .bashrc
  • GZIP_BINDIR is the variable – Maythux May 27 '15 at 10:30
  • what is not right? No i didn't? mine is downvoted also – Maythux May 27 '15 at 10:34
  • Why downvoting?! – Maythux May 27 '15 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.