Is "${PS1-}" valid syntax and how does it differ from plain "$PS1"?

The variable expansion ${parameter:-word} will use the value of $parameter if it's set and non-null (not an empty string), otherwise it will use the string word.

Omitting the : will not test if the value is empty, only whether it's unset or not.

This means that ${PS1-} will expand to the value of $PS1 if it's set, but to an empty string if it's empty or unset. In this case, this is exactly the same as ${PS1:-} as the string after - is also empty.

The difference between "${PS1-}" and "$PS1" is subtle, as @Rakesh Sharma notes: both will expand to the value of $PS1, or to an empty string if it is unset. The exception is when set -u is active, in which case expanding unset variables would cause an error. The (empty) default value set by"${PS1-}" circumvents this, expanding an unset PS1 to the empty string without error.

This is standard syntax (originated in the Bourne shell in the late 70s), as are a couple of other, similar expansions.


This is definitely POSIX syntax. Paraphrasing:

Using ${parameter-word}, if parameter is

  • set and not null, then substitute the value of parameter,
  • set but null, then substitute null, and
  • unset, then substitute word.

Example session:

$ echo "${parameter-word}"
word
$ parameter=
$ echo "${parameter-word}"

$ parameter=value
$ echo "${parameter-word}"
value

"Null" here simply means the empty string. There is no special null value in POSIX shells, in contrast to SQL, for example.

This is also documented in the "Parameter Expansion" section of man bash.


The syntax:

${parameter:-word}

and a special form:

${parameter-word}

is the valid syntax in POSIX shell, means using the default value word if parameter is unset or null.


Normally, with word is empty, then:

${parameter-}

and:

$parameter

or:

${parameter}

are equivalent.

But under the effect of set -u, all unset variables cause the shell to be terminated. ${parameter-} is used to bypass that strict rule. It works in both case so yes, it's more correct way.