Re: Difference between `` and $()



Chris F.A. Johnson wrote:
On 2007-06-15, Prateek wrote:
Hi, Can someone please help me understand the difference between
`cat file' and $(cat file) for korn shell. I thought they are teh
same. But my tests show they are not. For example

var=$(cat$(ls)) is different from var=`cat `ls``

var=$(cat $(ls)) is the same as var=`cat \`ls\``


I would refer to bash(1), there they explain the above.


Command Substitution
Command substitution allows the output of a command to replace
the command name. There are two forms:

$(command)
or
‘command‘

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. Command substitutions may be nested. To nest when using the
backquoted form, escape the inner backquotes with backslashes.

If the substitution appears within double quotes, word splitting and
pathname expansion are not performed on the results.

regards
Klaas
.



Relevant Pages

  • Re: Why are here no line feeds/CRs ?
    ... Command substitution allows the output of a command to ... with any trailing newlines deleted. ... When the old-style backquote form of substitution is used, ...
    (comp.os.linux.misc)
  • Re: Suchen und Ersetzen mit sed
    ... Das schafft leider auch keine Abhilfe: ... Command substitution allows the output of a command to be substituted ... Helmut Waitzmann, xxx@xxxxxxxxxxx ...
    (de.comp.os.unix.shell)
  • Re: ksh93 extensions in C
    ... I mean I can't capture the output by command substitution unless I ... redirect it, ... Command substitution does however work with builtins that are already ...
    (comp.unix.shell)
  • Re: horizontal separated record to vertical?
    ... Command substitution allows the output of a command to replace the com- ... Bash performs the expansion by executing command and replacing the com- ... mand substitution with the standard output of the command, ...
    (comp.unix.shell)
  • Re: Get rid off last char on last line
    ... Thanks Bill, don't know why but I saw the Ed post first. ... Another approach is to use the "fmt" command to reformat your command. ...
    (comp.unix.shell)