Comportamento diferente de $ () e '' [duplicado]

4
    

Esta pergunta já tem uma resposta aqui:

    
% PATH="MYPATH"
% VAR="PATH"

% echo $(eval echo \$$VAR)
MYPATH

% echo 'eval echo \$$VAR'
5707VAR
 ^^
This is the process number.

Eu achei que os dois eram exatamente iguais, mas obviamente existem algumas diferenças, como fugir do comportamento. Quais são as diferenças?

    
por Profpatsch 15.03.2013 / 12:20

1 resposta

7

Vou reproduzir o texto do manual de referência BASH porque não vou expressar melhor:

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by ‘$’, ‘'’, or ‘\’. The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

Fonte: Manual de referência de bash, substituição de comando

    
por 15.03.2013 / 12:26