68

I am currently trying out the fish shell instead of using bash. One type of notation I'm having trouble learning the fish-equivalent notation for is $(command), similar to how it is described in this SOF post. How do I write this using fish? Keep in mind that I could use backslash characters around the command I want to evaluate, but the linked post and other posts discourage this because it is an old style of evaluating commands.

Specifically, this is the bash command I want to convert to fish syntax (for initializing rbenv during startup of the shell):

eval "$(rbenv init -)"
asked Oct 6, 2013 at 17:42
1
  • 4
    Per this entry in the fish FAQ, sub-commands are denoted by surrounding them with parenthesis. e.g., set foo (echo bar); echo $foo outputs bar. Commented Oct 6, 2013 at 19:50

4 Answers 4

89

In fish, $ is used only for variables. Correct notation equivalent to bash $(command) is just (command) in fish.

BuZZ-dEE
7,18316 gold badges75 silver badges105 bronze badges
answered Jan 4, 2014 at 8:58
Sign up to request clarification or add additional context in comments.

1 Comment

eval (docker-machine env default --no-proxy --shell=fish) :D thanks for the help too
20

FYI: If you additionally need to use this inside a string:

echo "Found "(count $PATH)" paths in PATH env var"
answered Apr 13, 2020 at 15:28

1 Comment

So there is no way around temporarily closing the quote and reopening right after. fishshell.com/docs/current/tutorial.html#command-substitutions
10

Since fish 3.4 (released March 2022), $()-substitution is supported. It works the same as ()-substitution, but can be used inside double-quoted strings.

answered Jul 13, 2022 at 9:02

2 Comments

I want to use ``` alias math=math ()" ``` so that I can use brackets in the calculation by default. Is this doable?
@Pranav please ask a new question for the best chance of someone writing an answer :)
0

Watch out that () and $() in fish are not totally equivalent to $() in bash.

Fish only splits command substitutions on newlines.

  • In fish cmd $(echo "A B C") is equivalent to cmd 'A B C', but
  • in bash cmd $(echo "A B C") is equivalent to cmd A B C.

But cmd $(echo -e "A\nB\nC") is indeed equivalent in fish and bash.

answered Nov 28, 2023 at 9:09

Comments

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.