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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With