ecbrodie
ecbrodie

Reputation: 11896

Fish equivalent of bash $(command) notation

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 -)"

Upvotes: 68

Views: 32354

Answers (4)

Sorashi
Sorashi

Reputation: 931

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.

Upvotes: 0

ash
ash

Reputation: 5529

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

Upvotes: 10

Phlogisto
Phlogisto

Reputation: 1016

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

Upvotes: 89

JairoV
JairoV

Reputation: 2094

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

echo "Found "(count $PATH)" paths in PATH env var"

Upvotes: 19

Related Questions