There is another question about testing command substitution for a particular string. I want to test if a command outputs anything in a single statement, i.e., the equivalent of:
if [[ -n "$(foo)" ]]
in bash. Fish doesn't recognize [[]], and
if [ -n "(foo)" ] # No good, "(foo)" is literal.
if [ -n (foo) ] # Passes only if (foo) == "\n" because of test semantics.
Won't work meaning I have to
set check (foo)
if [ -n "$check ]
Is there a possibility I've missed here?
asked Mar 11, 2015 at 9:17
wholerabbit
11.6k2 gold badges40 silver badges73 bronze badges
3 Answers 3
This should work:
if count (foo) > /dev/null
answered Mar 11, 2015 at 11:59
ridiculous_fish
19k1 gold badge63 silver badges71 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
wholerabbit
Out of curiosity, do you think this would be more or less expensive than
if [ (count (foo)) != 0 ]? I'm presuming the > /dev/null version saves a subshell...ridiculous_fish
@goldilocks About equal - fish isn't yet smart enough to elide the fork when outputting from a builtin to /dev/null, though it ought to be. If it were to elide the fork, the count command would be more efficient.
As far as I know there is no way to use string substitution in Fish.
You can follow #159 issue to get more info about current solutions.
answered Mar 11, 2015 at 9:50
Hauleth
23.7k4 gold badges67 silver badges117 bronze badges
Comments
In fish, we have to use test instead [ or [[
if test -n (foo)
echo not empty
end
Or, the equivalent of the bash [[ -n $(foo) ]] && echo not empty is
test -n (foo); and echo not empty
answered Mar 11, 2015 at 12:14
glenn jackman
249k42 gold badges233 silver badges363 bronze badges
1 Comment
wholerabbit
[ ] is test. Try your example with, e.g. if test -n (echo -n "") -- You get "Not empty". test -n (aka. [ -n ]) will only fail against "\n". A literally empty string (no output, as echo -n "") will pass.