Command substitution failure - why?

Cameron Simpson cs at zip.com.au
Mon Nov 15 00:46:17 UTC 2004


On 17:51 14 Nov 2004, C. Linus Hicks <lhicks at nc.rr.com> wrote:
| On Sun, 2004-11-14 at 15:56 -0600, Vidiot wrote:
| [linush at lh4 ~]$ set -x
| ++ echo -ne '\033]0;linush at lh4:~\007'
| [linush at lh4 ~]$ OUT=`echo ann001.jpg | awk "{ print substr(\$0, 1, length(\$0) -7 ) }"`
| ++ awk '{ print substr(bash, 1, length(bash) -7 ) }'
| ++ echo ann001.jpg
| + OUT=
| ++ echo -ne '\033]0;linush at lh4:~\007'
| 
| Notice that your "$0" was getting substituted with the wrong thing. I
| changed your double quotes to single so it won't get interpreted by the
| shell:
| 
| [linush at lh4 ~]$ OUT=`echo ann001.jpg | awk '{ print substr(\$0, 1, length(\$0) -7 ) }'`
| ++ awk '{ print substr($0, 1, length($0) -7 ) }'
| ++ echo ann001.jpg
| + OUT=ann
| ++ echo -ne '\033]0;linush at lh4:~\007'

And this is because backticks add another layer of sloshing.

You can try using the $() syntax, thus:

	OUT=$(echo ann001.jpg | awk '{ print substr(\$0, 1, length(\$0) -7 ) }')

it has the advantage of not introducing a new level of sloshing because the
$(...) stuff nests nicely.
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

It is interesting to think of the great blaze of heaven that we winnow
down to animal shapes and kitchen tools.        - Don DeLillo




More information about the redhat-list mailing list