11

In sh:

~$ `echo ls`
bin/  Desktop/

But in fish:

fish: Illegal command name “(echo ls)”
~% (echo ls)

(Note that the error message appears above the command line.)

~% echo (echo ls)
ls
~% eval (echo ls)
bin/  Desktop/

fish: Illegal command name “(echo ls)”
exec (echo ls)
     ^
~% exec (echo ls)

It seems that command substitution only works as parameters of a command, not as a command itself? Why?

Well, the help doc does say

If a parameter contains a set of parenthesis, the text enclosed by the parenthesis will be interpreted as a list of commands.

But still, why?

3
  • Since the error message appears above the command line and it appears that you then edited it before resubmitting it, it's confusing since none of the commands you show produce that error. Perhaps you should insert (echo ls) after the error message for clarity. Commented Jul 19, 2010 at 14:04
  • Sorry, I miss the '()'. Edited.
    – weakish
    Commented Jul 19, 2010 at 15:00
  • I've starting doing it like this echo ls | bash Commented Jan 29, 2020 at 20:53

2 Answers 2

11

Update

This answer was written ten year ago in 2010. Recent versions of fish (I tested on 3.1.2) updated and set cmd ls; $cmd is valid now.

How

This because command substitutions belong to parameter expansions and are not allowed as commands.

A similar example:

in sh:

tmpls=ls
$tmpls

But in fish:

% set cmd ls; $cmd
fish: Variables may not be used as commands.
...

Why

In short, it's good for verifiability

This article explains details:

Since it is allowed to use variables as commands in regular shells, it is impossible to reliably check the syntax of a script. For example, this snippet of bash/zsh code may or may not be legal, depending on your luck. Do you feel lucky?

    if true; then if [ $RANDOM -lt 1024 ]; then END=fi; else END=true; fi; $END

Both bash and zsh try to determine if the command in the current buffer is finished when the user presses the return key, but because of issues like this, they will sometimes fail. Even worse, this piece of perfectly legal code is rejected by bash:

  FI=fi; foo() { if true; then true; $FI; }

Fish avoids this kind of problem, since variables are not allowed as commands. Anything you can do with variables as commands can be done in a much cleaner way using either the eval command or by using functions.

For the same reason, command substitutions are not allowed as commands.

(Note: The cited example is not fair, since 'if' and 'fi' are not simple commands but reserved words. See comments below.)

5
  • For a discussion of this issue regarding Bash, see BashFAQ/050. Commented Jul 19, 2010 at 17:48
  • 2
    the article you cite in your "why" section is plain wrong (at least as far as this section is concerned). A command like FI=fi; foo() { if true; then true; $FI; } is not valid shell syntax, so it is correctly flagged as a syntax error by all POSIX shells. Words like fi are reserved words, and they are recognized before word expansion which expands $FI to fi. Commented Jul 19, 2010 at 19:39
  • tldr; "Anything you can do with variables as commands can be done in a much cleaner way using either the eval command..." Commented Aug 20, 2014 at 19:13
  • The example that you gave is perfectly valid fish code. Commented Nov 17, 2020 at 2:15
  • 1
    @Sapphire_Brick It was invalid ten years ago. Thanks for reminding me to update the answer.
    – weakish
    Commented Nov 17, 2020 at 11:03
0

It has to do with the order of expansions.

From help expand-command-substitution in fish:

When combining multiple parameter expansions, expansions are performed in the following order:

 * Command substitutions
 * Variable expansions
 * Bracket expansion
 * Pid expansion
 * Wildcard expansion

Expansions are performed from right to left, nested bracket expansions are performed from the inside and out.

From man bash:

The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.

1
  • I'm still confusing. It seems that fish doesn't try to expand the Command substitution, and takes (echo ls) literally as a command.
    – weakish
    Commented Jul 19, 2010 at 15:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.