64

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 -)"
Community
  • 1
  • 1
ecbrodie
  • 11,246
  • 21
  • 71
  • 120
  • 4
    Per [this](http://fishshell.com/docs/2.0/faq.html#faq-subcommand) entry in the fish FAQ, sub-commands are denoted by surrounding them with parenthesis. e.g., `set foo (echo bar); echo $foo` outputs `bar`. – iscfrc Oct 06 '13 at 19:50

3 Answers3

85

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

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Phlogisto
  • 976
  • 8
  • 6
17

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

echo "Found "(count $PATH)" paths in PATH env var"
  • So there is no way around temporarily closing the quote and reopening right after. https://fishshell.com/docs/current/tutorial.html#command-substitutions – aasutossh Jun 04 '21 at 15:46
7

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

ash
  • 5,139
  • 2
  • 27
  • 39
  • I want to use ``` alias math=math ()" ``` so that I can use brackets in the calculation by default. Is this doable? – Pranav Feb 19 '23 at 00:02
  • @Pranav please [ask a new question](https://stackoverflow.com/questions/ask) for the best chance of someone writing an answer :) – ash Feb 19 '23 at 21:18