Skip to content

Echo chokes on unset variables but does not set an error code. #352

Closed

Description

@Soares
> set x x
> set y y
> echo $x$y
xy

Cool. Now let's try echo on unset variables:

> echo $z 
> echo $status
> 0

Fine. Now:

> echo $x$z

> echo $status
0

!?

Echo should either output x (the only variable that is set) or die with an error message. It shouldn't silently output nothing!

Activity

  1. maxfl commented on Oct 18, 2012

    @maxfl
    Contributor

    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.

  2. Soares commented on Oct 18, 2012

    @Soares
    ContributorAuthor

    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.

  3. ridiculousfish commented on Oct 18, 2012

    @ridiculousfish
    Member

    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.

  4. Soares commented on Oct 18, 2012

    @Soares
    ContributorAuthor

    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 '').

  5. removed this from the fish-future milestone on Feb 5, 2016
  6. locked as resolved and limited conversation to collaborators on Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions