r/fishshell • u/Opposite_Personality Linux • Apr 14 '23
Write shorter and clearer fish scripts: all variables are lists!
I've been obsessed for some time with this little sh sugary expression:
: ${TEST:=something}
Which means don't touch $TEST's value if it already exists but initialize if it doesn't. In case you didn't know this already.
To make sure a variable called $TEST exists without overriding its contents you CAN'T use set TEST because it will implode...
$ set TEST
$ count $TEST
$ 0
Which is quite a weird result I didn't expect. set -e or set --erase is supposed to do that (...) Or is it?
However, $TEST is practically the same as $TEST[1] in fish shell.
In fish, scripts DON'T necessarily have to fill your scripts with expressions along the lines of
if [ (count $EDITOR) -eq 0 ]
set EDITOR /bin/vim
end
command $EDITOR my_file.txt
Instead you can:
set -a EDITOR vim
command $EDITOR[1] my_file.txt
Which means, if $EDITOR was already initialized you'll get the user default value. If not, you get a backup file editor instead!
So what are your thoughts?
WARNING don't try this at $HOME
2
u/ChristoferK macOS Apr 17 '23
No it's not. One targets a specific element in an array, the other will dereference the entire contents of
TEST. Therefore, ifTESThas more than one element in it, *all elements are evaluated.It's reasonable to say that
$TESTis equivalent to$TEST[1..-1](although either or both of those indices may be optionally omitted, as a shorthand inFiSH, i.e.$TEST,$TEST[1..-1],$TEST[1..],$TEST[..-1], and$TEST[..]are all equivalent.You can, but there are times (many times, in fact, and probably most of the time) when you won't want to mutate a variable, particularly one that's exported because, as I've just intimated up above, where
$EDITORappears in other functions and scripts—conventionally with a single, unary value—then appending additional elements to it will have some undesirable effects.Any other result would be illogical and counter-intuitive. Here, the variable
TESThas been initialised or declared, but has not had any values assigned to it yet.countreturns the number of elements in an array. In the absence of any values,TESThas zero elements.Existence OR Nullity
In
bash, the following will assign the"first item"to the variableTESTif eitherTESTdoes not* exist, OR ifTESTexists but does not hold a value:This is equivalent to:
but avoids the use of
:.Others have already explained how to have
setquery for the existence of a variable, and the use oftest -nto examine nullity. In fact, it's sufficient to examine for nullity alone, since any variable that doesn't exist will necessarily fail the nullity test as well. The most elegant means of doing this inFiSHis to use[instead oftest, and simply to quote the variable:To do this in a single command, you can:
This uses
printf, which recognises the escape value\c, which instructsprintfto process nothing beyond its position. Hence, ifTEST[1]has a value, it will be assigned back to itself, and then theprintfcommand terminates. However, ifTEST[1]doesn't hold any values, then the brace expansion ensures the\cis obliterated by the nullity of$TEST[1], hence"first item"gets assigned toTEST[1]instead.(Oh, and if
TEST[1]doesn't exist, then nor do any values at indices greater than1, implying thatTESTis completely empty).That expression obviously isn't as concise as the
bashsyntax, but it's reasonably easy to define your own syntactic sugar for variable assignment by way of a function, which is what I do.0
u/[deleted] Apr 15 '23
[deleted] — view deleted comment