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
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:
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
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.
Reacted by Raman Gupta, Josh Ourisman, Rafael Epplée, Spencer Yue and Mikel Ward
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.
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?
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.
@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.
@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
@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.
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:
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.
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
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.
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
Activity
ridiculousfish commentedon Apr 24, 2015
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_CLIENTis not set, thentest -n $SSH_CLIENTis equivalent totest -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:Or maybe a little simpler:
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.
EricG-Personal commentedon Apr 24, 2015
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.
zanchey commentedon Apr 27, 2015
I think the better documentation was added as part of #1574 (which is not in a release yet).
brandonweiss commentedon Jul 28, 2015
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.
I'm guessing the reason this doesn't work is because the command substitution returns nothing? So
test -nis always true? What's the correct way to write this?ridiculousfish commentedon Jul 28, 2015
This should do it:
But really you don't need the
ifat all:since the exit status suffices.
brandonweiss commentedon Jul 28, 2015
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>&1Otherwise the output that was previously being capture by the command substitution is sent to stdout.
ridiculousfish commentedon Sep 23, 2015
Certainly nobody here is in love with POSIX :)
Nobody runs
test -nby itself. They either runtest -n $someEmptyVariableortest $someVariableThatIsDashN. We might want to distinguish those.ridiculousfish commentedon Sep 23, 2015
Right, so one possibility is that we teach
testabout empty expansions.GReagle commentedon Sep 24, 2015
If you're writing in fish, why not do it the better fish way?
faho commentedon Sep 24, 2015
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.eugenesvk commentedon Jan 19, 2016
@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.
6 remaining items
GReagle commentedon Mar 7, 2016
@anordal:
=and!=have the same problem as-nand-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:anordal commentedon Mar 7, 2016
@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
-nand-zwere fraught with peril, and been too scared to ever use them.Remove `test` invocations with implicit `-n` tests
floam commentedon Jan 28, 2019
Well, 👎 from me.
By removing the one-argument case of
testwe'd break stuff. The single argument case is useful, for example a solution given above by @ridiculousfish with command substitution:Trying something like that would be harder with
-n.testreally should not depart from POSIX in this core behavior.floam commentedon Jan 28, 2019
We need to document the precise behavior
testhas 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
faho commentedon Mar 5, 2019
@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
testdeviate 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 keeptestaround.To be frank:
testis 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 (testand[ ])...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.GReagle commentedon Mar 6, 2019
@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
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?
Darkshadow2 commentedon Mar 6, 2019
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.faho commentedon Mar 6, 2019
@GReagle
Yes. (Also since 2.3.0 you don't need the
beginandendhere anymore -andandorbind to the if-condition)string lengthreturns true if any of its arguments is not empty, so this can just bestring length -q -- $SSH_CLIENT $SSH_TTY.GReagle commentedon Mar 6, 2019
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 withandandorlike so,if set -q var1; or set -q var2; echo either defined; endKeep in mind that a defined variabled could be empty. Both of these commands define an empty variable:
set varandset 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 thatstring lengthwill interpret a list of multiple variables as a disjunction (meaning any/or).