Skip to content

test -n not working as expected #2037

Closed

Description

@EricG-Personal

I have the following block:

if begin; test -n $SSH_CLIENT; or test -n $SSH_TTY; end
echo "ssh (0)"
else
echo "not ssh (0)"
end

If I am correct, it should only print "ssh" if either of the environment variables $SSH_CLIENT of $SSH_TTY are defined. To verify that neither are defined:

   eric@ubuntu ~> printenv | grep SSH
   SSH_AUTH_SOCK=/run/user/1000/keyring-bKvxzV/ssh

and yet the output I get is "ssh". If I reverse the code block and do:

if begin; test -z $SSH_CLIENT; and test -z $SSH_TTY; end
echo "not ssh (1)"
else
echo "ssh (1)"
end

It works as expected.

So, running these two blocks where neither $SSH_CLIENT of $SSH_TTYare defined, the output I get is:

ssh (0)
not ssh (1)

and running these two blocks where both $SSH_CLIENT of $SSH_TTY are defined, the output I get is:

ssh (0)
ssh (1)

The -n flag does not work. The -z flag does.

> fish --version
fish, version 2.1.1

> uname -a
Linux ubuntu 3.16.0-30-generic #40~14.04.1-Ubuntu SMP Thu Jan 15 17:43:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

Activity

  1. ridiculousfish commented on Apr 24, 2015

    @ridiculousfish
    Member

    I thought we had a bug on this somewhere, but I can't find it. Anyways see my StackOverflow answer for this question.

    The short summary is that if $SSH_CLIENT is not set, then test -n $SSH_CLIENT is equivalent to test -n, and POSIX requires that we just check if that one argument (the -n) is not null. You can fix it by quoting, which forces an argument even if it's empty:

    if begin; test -n "$SSH_CLIENT"; or test -n "$SSH_TTY"; end
    

    Or maybe a little simpler:

    if test -n "$SSH_CLIENT" -o -n "$SSH_TTY"
    

    This screws people up enough that we might want to break from POSIX here. If you like, I can leave this open to track that.

  2. EricG-Personal commented on Apr 24, 2015

    @EricG-Personal
    Author

    No, I would suggest sticking with POSIX. What may be a good solution here is to simply add some additional examples to the documentation to make this problem and solution easier to spot.

  3. zanchey commented on Apr 27, 2015

    @zanchey
    Member

    I think the better documentation was added as part of #1574 (which is not in a release yet).

  4. added this to the fish-future milestone on Jun 14, 2015
  5. brandonweiss commented on Jul 28, 2015

    @brandonweiss

    facepalm

    This has been driving me nuts for the last several days. Let's say I have a function that tests whether or not I'm inside a git repository.

    function _in_git_directory
      if test -n (git rev-parse --git-dir ^ /dev/null)
        true
      else
        false
      end
    end
    

    I'm guessing the reason this doesn't work is because the command substitution returns nothing? So test -n is always true? What's the correct way to write this?

  6. ridiculousfish commented on Jul 28, 2015

    @ridiculousfish
    Member

    This should do it:

    function _in_git_directory
        if test (git rev-parse --git-dir ^ /dev/null)
                true
            else
                false
        end
    end
    

    But really you don't need the if at all:

    function _in_git_directory
        git rev-parse --git-dir ^ /dev/null
    end
    

    since the exit status suffices.

  7. brandonweiss commented on Jul 28, 2015

    @brandonweiss

    Interesting! I was way over-complicating it. I think the second case will only work for me if I redirect everything to /dev/null.

    git rev-parse --abbrev-ref @"{u}" > /dev/null 2>&1

    Otherwise the output that was previously being capture by the command substitution is sent to stdout.

  8. ridiculousfish commented on Sep 23, 2015

    @ridiculousfish
    Member

    Certainly nobody here is in love with POSIX :)

    Nobody runs test -n by itself. They either run test -n $someEmptyVariable or test $someVariableThatIsDashN. We might want to distinguish those.

  9. ridiculousfish commented on Sep 23, 2015

    @ridiculousfish
    Member

    Right, so one possibility is that we teach test about empty expansions.

  10. GReagle commented on Sep 24, 2015

    @GReagle
    Contributor

    If you're writing in fish, why not do it the better fish way?

    if begin; set -q SSH_CLIENT; or set -q SSH_TTY; end
    
  11. faho commented on Sep 24, 2015

    @faho
    Member

    Because it can still be empty, even if it is defined. set SSH_CLIENT; set -q SSH_CLIENT; and count $SSH_CLIENT - though, as we see here, count can be used instead.

  12. eugenesvk commented on Jan 19, 2016

    @eugenesvk

    @ridiculousfish nice that I found your solution with quoting variables and thanks @bucaran for your solution for expressions. Thanks!
    But that's quite a non-intuitive solution. I'm all up for breaking from POSIX to fix it.

  13. 6 remaining items

  14. GReagle commented on Mar 7, 2016

    @GReagle
    Contributor

    @anordal: = and != have the same problem as -n and -z: they require the variable to be quoted to ensure a reliably correct result. If the variable is not quoted, they fail with error if the variable is either unset or an array:

    set SSH_CLIENT a b c; if test $SSH_CLIENT != ''; echo ssh; end
    set -e SSH_CLIENT; if test $SSH_CLIENT != ''; echo ssh; end
    
  15. anordal commented on Mar 7, 2016

    @anordal
    Contributor

    @GReagle: Sorry, I actually knew that, but didn't think of it as a problem — it works with quotes anyway. AFAIK, every variable in fish is an array.

    Maybe it's just me, but I've always thought that -n and -z were fraught with peril, and been too scared to ever use them.

  16. floam commented on Jan 28, 2019

    @floam
    Member

    @faho, @krader1961: Sounds to me like a dilemma, and that the safest fix is to remove the feature!

    Well, 👎 from me.

    By removing the one-argument case of test we'd break stuff. The single argument case is useful, for example a solution given above by @ridiculousfish with command substitution:

    if test (git rev-parse --git-dir ^ /dev/null)

    Trying something like that would be harder with -n.

    test really should not depart from POSIX in this core behavior.

  17. floam commented on Jan 28, 2019

    @floam
    Member

    We need to document the precise behavior test has with 1 argument, 2 arguments, 3 arguments, 4 arguments, because it's not exactly clear from our man-page and that's not helpful. Like this does:

    http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html

  18. faho commented on Mar 5, 2019

    @faho
    Member

    The single argument case is useful, for example a solution given above by @ridiculousfish with command substitution:

    @floam: If this misbehavior wasn't in there, that wouldn't have to use the single-argument! (Also it's broken if the command substitution ever returns more than one argument)


    So, the alternatives here are:

    • Make test deviate by not doing automatic "-n", both for the single-argument and argument-after-combiner cases (test something -a somethingelse).

    • Implement an improvement on test, but probably keep test around.

    To be frank: test is pretty crappy no matter what we do. The combiners interact poorly with lists and command substitutions, the operators are weirdly spelt, it's got two names (test and [ ])...

    If we removed this piece of behavior, we'd lose some more compatibility (though it's pretty uncommon to write test something), and it wouldn't be backwards-compatible. But it might still be worth it.

  19. GReagle commented on Mar 6, 2019

    @GReagle
    Contributor

    @EricG-Personal and @faho

    The original post has "if either of the environment variables $SSH_CLIENT of $SSH_TTY are defined". So my reply of

    if begin; set -q SSH_CLIENT; or set -q SSH_TTY; end
    

    is a correct and fine way to determine whether either variable is defined, right? Yes I know that defined could be empty.

    To consider the question of how to check whether either of two variables is not empty, here is my attempt to do so without using the test command. Is it correct? Is there a better way without using test?

    if begin; string length -q -- $SSH_CLIENT; or string length -q -- $SSH_TTY; end
        echo "not empty"
    end
    
  20. Darkshadow2 commented on Mar 6, 2019

    @Darkshadow2
    Contributor

    That works, but you can also use set -q SSH_CLIENT[1]; or set -q SSH_TTY[1]. The only time those will pass unexpectedly is if they are specifically set to an empty string (like this: set SSH_CLIENT ""). Otherwise, if they're defined but have no value, it will fail.

  21. faho commented on Mar 6, 2019

    @faho
    Member

    @GReagle

    is a correct and fine way to determine whether either variable is defined, right? Yes I know that defined could be empty.

    Yes. (Also since 2.3.0 you don't need the begin and end here anymore - and and or bind to the if-condition)

    either of two variables is not empty

    string length -q -- $SSH_CLIENT; or string length -q -- $SSH_TTY

    string length returns true if any of its arguments is not empty, so this can just be string length -q -- $SSH_CLIENT $SSH_TTY.

  22. GReagle commented on Mar 6, 2019

    @GReagle
    Contributor

    Thank you @faho for improving the code and my understanding of fish. I suggest we add to the FAQ then, what do you think?

    Q: How do I check whether a variable is defined?

    Use set -q var. For example, if set -q var; echo defined; end. To check multiple variables you can combine with and and or like so, if set -q var1; or set -q var2; echo either defined; end

    Keep in mind that a defined variabled could be empty. Both of these commands define an empty variable: set var and set var "".

    Q: How do I check whether a variable is not empty?

    Use string length -q -- $var. For example, if string length -q -- $var; echo not empty; end. Note that string length will interpret a list of multiple variables as a disjunction (meaning any/or).

    if string length -q -- $var1 $var2 $var3
        echo at least one of these variables is not empty
    end
    
  23. locked as resolved and limited conversation to collaborators on Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions