Let's say we have a command output a variable assignment string, say 'var=foo', if I put this command in Command Substitution which looks like
$(echo var=foo), it causes 'command not found' error.
[223212 dot@anne ~]$ var=foo
[223226 dot@anne ~]$
[223230 dot@anne ~]$ $(var=foo)
[223235 dot@anne ~]$
[223236 dot@anne ~]$ $(echo var=foo)
bash: var=foo: command not found
[223240 dot@anne ~]$
[224909 dot@anne ~]$ $(echo ls)
a b c d
[225036 dot@anne ~]$
[225110 dot@anne ~]$ $(echo $(var=foo))
[225116 dot@anne ~]$
Since we can directly put variable assignment in Command Substitution like this $(var=foo) (though it's meaningless I think), and $(echo ls) works as expected as well, why output a assignment in Command Substitution causing error?
This is man bash about Command Substitution:
Command substitution allows the output of a command to replace the command name.
Bash performs the expansion by executing command in a subshell environment and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.
As I understand it, $(echo var=foo) should be replace by var=foo just like $(var=foo).
Am I getting it wrong?
foo=$(echo buzz). Of course "echo" doesn't make such sense here, it doesn't require a command substitution usually, you would normally just usefoo=buzzvar\=foo, thus the command not found error. Try to eval the result to fix this:eval $(echo var=foo).$(pgp -qd file), then you're presumably running into the issues described in detail in BashFAQ #50 (as a command generated from an unquoted command substitution has the exact same caveats as one generated via the unquoted expansion of a string).evalmay be well what the OP wants, but it's dangerous and best not recommended without explicit caveats.