43

In bash I can say:

$ echo "a$(echo b)c"
abc

How do I do this in the fish shell?

Joshua Cheek
  • 30,436
  • 16
  • 74
  • 83

1 Answers1

65
echo a(echo b)c

If you have quotes, you must exit them:

echo "a"(echo b)"c"

If your subcommand may have newlines, as of fish 2.3, you have to save and restore $IFS:

set -l IFS 
echo "a"(cat ~/file.txt)"c"
set -e IFS

Eventually string will be able to handle this case.

ridiculous_fish
  • 17,273
  • 1
  • 54
  • 61
  • 1
    This will somehow swallow all newline characters and does not seem to be equivalent. Minimal example: Given `echo -e "a\nb" > x` then bash `echo "$(cat x)"` does the right thing whereas fish `echo (cat x)` will transform the newline to a space (which would be equivalent to bash `echo $(cat x)` without the quotes). – Debilski Dec 09 '15 at 11:43
  • Is there still no way of doing this other than setting `IFS` to empty? – user14492 Aug 12 '20 at 08:25
  • `string collect` is now available to avoid newline splitting. Example usage: `echo (cat x | string collect)` – ridiculous_fish Aug 13 '20 at 18:47