From Bash Manual:

command [-pVv] command [arguments ...]

Runs command with arguments ignoring any shell function named command. Only shell builtin commands or commands found by searching the PATH are executed. If there is a shell function named ls, running ‘command ls’ within the function will execute the external command ls instead of calling the func- tion recursively. The -p option means to use a default value for PATH that is guaranteed to fi nd all of the standard utilities. The return status in this case is 127 if command cannot be found or an error occurred, and the exit status of command otherwise.

Does the manual explain why it fails when command is assignment or begins with assignment (for environment variables)?

$ command aaa=1
aaa=1: command not found

$ command aaa=1 echo hello
aaa=1: command not found
  • 4
    Because an assignment is not a command? – muru Apr 22 '16 at 21:54
  • 2
    See my first comment. – muru Apr 22 '16 at 21:57
  • 1
    it doesn't answer my last comment. – Tim Apr 22 '16 at 21:57
  • 2
    Yes, it does.,. – Tim Apr 22 '16 at 21:58
  • 3
    I understand your curiosity, Tim, but when your question is "Does the manual explain why it fails when (I do something)?" I think you're asking for our opinion of the manual, and expecting the manual to explain something above and beyond the shell's syntax. Is it not clear to you from the shell's error message that it's trying to execute the intended variable assignment? – Jeff Schaller Apr 23 '16 at 1:40

You are confusing what POSIX calls a "simple command" which is a non empty sequence of optional assignments, optional redirections and optional words (including a command name and its optional arguments), and "command" as it is used in the Bash manual command synopsis which is here just a command name.

Should you really want to use assignments here, you can simply run :

aaa=1 command echo hello

If there is no command at all but just an assignment, there is no much point using the command command, given the fact there is no builtin or command to search in the PATH in the first place.

Should you really want to just set a variable with command, you might use

command typeset aaa=1


command declare aaa=1
  • 2
    I wish I could give this more than one upvote. You said exactly what I was thinking. – Wildcard Apr 22 '16 at 22:29

Use the "let" command (for arithmetic expressions)

The command form of the plain assignment aaa=1 is let aaa=1. However, this applies to arithmetic expressions only (and not to string assignments):

$ command let aaa=1
$ echo $aaa
$ command let aaa=6*3+5
$ echo $aaa
  • 2
    You are misusing let here, which evaluates arithmetic expressions (or perhaps confusing it with the BASIC let instruction). command let aaa=hello would set aaa to 0 which is unlikely what the OP expects.. – jlliagre Apr 22 '16 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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