Shell – how to reference a variable in fish shell with a default fallback

fishshell

What is the fish shell equivalent to the following bash:

echo ${TEST:-1}

In bash this would print the value of TEST if there was one, or 1 if there was not.

Best Answer

I believe you need temponary variable (or a function). If $TEST doesn't exist, it's expanded to nothing, and $test_or only contains 1. It would be nice to have real way of doing that, but currently the syntax is issue, and it doesn't appear it's really needed for most purposes.

set test_or $TEST 1
echo $test_or[1]

Also, if all you want is to modify $TEST if it doesn't exist already, you can use set -q.

set -q TEST; or set TEST 1
echo $TEST
Related Question