bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Command substition failure?


From: Michael Witten
Subject: Command substition failure?
Date: Sat, 02 Apr 2011 12:36:30 +0000

Could somebody please tell me what's going on here?

First, here is a minimal example:

  LINE|
  0000| $ echo "$(echo '"' | awk '{sub(/a/,$0)}')"
  0001| awk: cmd. line:1: sub(/a/
  0002| awk: cmd. line:1:        ^ unexpected newline or end of string
  0003| awk: $0)
  0004| awk:   ^ syntax error
  0005|  

Where is this newline coming from? What happened to the comma?

Look what happens if we add the action `NR=1'; notice that the error
message seems to show that the `{' has been removed:

  LINE|
  0000| $ echo "$(echo '"' | awk 'NR==1 {sub(/a/,$0)}')"
  0001| awk: cmd. line:1: NR==1 sub(/a/
  0002| awk: cmd. line:1:              ^ unexpected newline or end of string
  0003| awk: NR==1 $0)
  0004| awk:         ^ syntax error
  0005|  

Interestingly, the whole thing seems to become syntactically correct
when I put a space between the `{' and the `sub' (and experimentation
shows that it is working):

  LINE|
  0000| $ echo "$(echo '"' | awk 'NR==1 { sub(/a/,$0)}')"
  0001| 

The problem also goes away when I just remove the outer quotes:

  LINE|
  0000| $ echo $(echo '"' | awk 'NR==1 {sub(/a/,$0)}')
  0001| 

However, it should be noted that the outer quotes should not have an
effect on any characters within the command substition `$(...)';
consider what the POSIX shell documentation here:

   
http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_02_03

says:

  Enclosing characters in double-quotes ("") shall preserve the
  literal value of all characters within the double-quotes, with
  the exception of the characters dollar sign, backquote, and
  backslash, as follows:

  $
      The dollar sign shall retain its special meaning
      introducing parameter expansion (see Parameter
      Expansion), a form of command substitution (see
      Command Substitution), and arithmetic expansion
      (see Arithmetic Expansion).

      The input characters within the quoted string
      that are also enclosed between "$(" and the
      matching ')' shall not be affected by the
      double-quotes, but rather shall define that
      command whose output replaces the "$(...)" when
      the word is expanded. The tokenizing rules in
      Token Recognition , not including the alias
      substitutions in Alias Substitution , shall be
      applied recursively to find the matching ')'.
      ...
  ...

In particular:

  The input characters within the quoted string
  that are also enclosed between "$(" and the
  matching ')' shall not be affected by the
  double-quotes, but rather shall define that
  command whose output replaces the "$(...)" when
  the word is expanded.

>From what I can tell, bash follows the same semantics; the manual here:

  $ info '(bash)Double Quotes'

or here:

  
http://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html#Double-Quotes

says:

  3.1.2.3 Double Quotes
  .....................

  Enclosing characters in double quotes (`"') preserves the
  literal value of all characters within the quotes, with the
  exception of `$', ``', `\', and, when history expansion is
  enabled, `!'. The characters `$' and ``' retain their special
  meaning within double quotes (*note Shell Expansions::).

and the manual here:

  $ info '(bash)Command Substitution'

or here:

  
http://www.gnu.org/software/bash/manual/html_node/Shell-Expansions.html#Shell-Expansions

says:

  3.5.4 Command Substitution
  --------------------------

  ...
  ... When using the `$(COMMAND)' form, all characters between
  the parentheses make up the command; none are treated
  specially.

  ...

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

Could somebody please tell me what's going on here?

Sincerely,
Michael Witten



reply via email to

[Prev in Thread] Current Thread [Next in Thread]