Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quiet `bash: ${PS1@P}: bad substitution` message

Tags:

bash

On older versions of bash, some variable / parameter expansions don't exist.

An example is expanding a variable as a prompt string:

echo ${PS1@P}

On an older version of bash, even if I do:

$ echo "${PS1@P}" &> /dev/null

I still see:

bash: ${PS1@P}: bad substitution

Note that both STDOUT and STDERR were redirected above.

How do I silently expand a parameter and check if it worked?

like image 590
Tom Hale Avatar asked Sep 06 '25 06:09

Tom Hale


1 Answers

Redirecting STDERR works when using eval:

eval 'str="${PS1@P}";' 2>/dev/null || echo fail

At this point, either you'll see fail or the variable $str will contain the expansion of the prompt.

Notes:

As @Charles Duffy points out in the comments, surrounding in { } will abort on failure without printing fail in bash 4.3.48.

The other option is to use a subshell.

like image 107
Tom Hale Avatar answered Sep 07 '25 19:09

Tom Hale