What is the meaning of ${GZIP_BINDIR-'/bin'} in bash?

${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.


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


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

Tags:

Bash