2

I am seeing the following code in a bash script:

export PATH=/opt/rh/rh-python38/root/usr/local/bin:/opt/rh/rh-python38/root/usr/bin${PATH:+:${PATH}}

I do not understand the last part, where ${PATH:+:${PATH}} is manipulated.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
xis
  • 24,330
  • 9
  • 43
  • 59
  • See also: [StackExchange: What does ${PATH:+:${PATH}} mean?](https://unix.stackexchange.com/q/267506/310674) and [What is the different between PATH=:$PATH and PATH="$PATH:" and other export lines](https://stackoverflow.com/q/49193620/7939871) – Léa Gris May 29 '22 at 07:47

1 Answers1

4

It's to append : and $PATH, but only if $PATH is non-empty; otherwise, nothing gets added.

The syntax for the expansion is ${parameter:+word}, and it stands for

If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

In your example, word is :$PATH.

There is a subtlety around ${parameter:+word} vs. ${parameter+word}, where the former substitutes nothing if parameter is unset or null, and the latter substitutes nothing only if parameter is unset (but could be empty).

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116