You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Echo chokes on unset variables but does not set an error code. #352
echo command doesn't know if a variable is passed to it. 'echo $unsetvar' works as 'echo' without arguments since $unsetvar is not expanded to anything.
Modifying 'echo' as you suggest would introduce a complication.
The only solution I can think of is a variable $expansionstatus which would contain an array of expansion statuses for each expansion in a commandline.
Right. I'm not saying that echo should do anything different on unset vars. Notice the last example:
echo $x$z
$x is set, $z is not. I was saying that it should either output $x alone or exit with an error status. You're right that only the first option actually makes sense, and his how bash & friends all act.
builtin echo currently always returns success; there's no way to make it fail.
The weird behavior for unset variables is actually due to how fish handles arrays. For example:
> set x a
> set y b c d
> echo $x$y
ab ac ad
Let's get weirder and concatenate an array with another array:
> set x a b c
> set y 1 2 3
> echo $x$y
a1 b1 c1 a2 b2 c2 a3 b3 c3
You end up with the Cartesian product of all of the arrays. And this explains the behavior of unset variables: an unset variable is like an empty array, and the Cartesian product of anything with the empty set is empty.
Double quotes don't use the Cartesian product but just joins all the elements with spaces and then concatenates them:
> set x a b c
> set y 1 2 3
> echo "$x$y"
a b c1 2 3
So double quotes will do something closer to what you would expect.
Ok wow, that's... pretty cool, actually. And maybe a little scary. I'm still really used to thinking of variables as strings. I need to get into thinking of variables as lists. (In that mindset, the test -n $var thing is intuitive when $var is empty, I take back the suggestion that empty variables expand to '').
Activity
maxfl commentedon Oct 18, 2012
echo command doesn't know if a variable is passed to it. 'echo $unsetvar' works as 'echo' without arguments since $unsetvar is not expanded to anything.
Modifying 'echo' as you suggest would introduce a complication.
The only solution I can think of is a variable $expansionstatus which would contain an array of expansion statuses for each expansion in a commandline.
Soares commentedon Oct 18, 2012
Right. I'm not saying that echo should do anything different on unset vars. Notice the last example:
$xis set,$zis not. I was saying that it should either output$xalone or exit with an error status. You're right that only the first option actually makes sense, and his how bash & friends all act.ridiculousfish commentedon Oct 18, 2012
builtin echo currently always returns success; there's no way to make it fail.
The weird behavior for unset variables is actually due to how fish handles arrays. For example:
Let's get weirder and concatenate an array with another array:
You end up with the Cartesian product of all of the arrays. And this explains the behavior of unset variables: an unset variable is like an empty array, and the Cartesian product of anything with the empty set is empty.
Double quotes don't use the Cartesian product but just joins all the elements with spaces and then concatenates them:
So double quotes will do something closer to what you would expect.
Soares commentedon Oct 18, 2012
Ok wow, that's... pretty cool, actually. And maybe a little scary. I'm still really used to thinking of variables as strings. I need to get into thinking of variables as lists. (In that mindset, the
test -n $varthing is intuitive when$varis empty, I take back the suggestion that empty variables expand to'').