2

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

3 Answers 3

4

This should work:

if count (foo) > /dev/null
answered Mar 11, 2015 at 11:59
Sign up to request clarification or add additional context in comments.

2 Comments

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...
@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.
1

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

Comments

1

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

1 Comment

[ ] 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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.