command substitution

command substitution

Post by hpi » Thu, 02 Oct 2003 23:55:20



Hi,

I've got something I cannot explain maybe there's somebody out there who
can.
I simplified the problem to make it more clear. First of all some basics.

The $(...) structure can be used to do command substitution.
The `...` structure does the same thing.

example :

echo $(echo "1:2:3"|cut -d: -f2)       ->     2
echo `echo "1:2:3"|cut -d: -f2`          ->    2

the << structure can be used to replace standard input for a command with
fixed input

example

cat <<EOF
hello mister
EOF

result

hello mister

The best way to understand this is to see the input between the both EOF's
as being input in a input file and the file is being shown by cat.
So,

file a.txt contains
hello mister
cat a.txt -> hello mister

The strength of this construction is the fact it can be used in combination
with variables

var1=mister

cat <<EOF
hello ${var1}
EOF

result

hello mister

If I combine this with the command substitution I can do

var1=mister
var2=$(cat <<EOF
hello ${var1}
EOF)

var2 contains now: hello mister

I also can use single quotes around the variable

var1=mister
cat <<EOF
hello '${var1}'
EOF

result

hello 'mister'

NOW IT COMES

if I want to put this in var2 using command substitution I use

var1=mister
var2=$( cat <<EOF
hello '${var1}'
EOF)

var2 now contains !!!!!!!!!!   ->    hello "${var1}"

Note the single quotes are converted into double quotes and the variable is
not expanded.

NOT IT COMES BIGTIME
If I use the other construction to do command substitution I get the
following

var1=mister
var2=` cat <<EOF
hello '${var1}'
EOF`

var2 now contains   ->    hello 'mister'

This is what I expected the first time

The <<EOF construction doesn't do the conversion as shown above, so I would
conclude that the $(...) construction does this conversion.

NOW IT COMES AGAIN

If I do this:

var1=mister
var2=$(echo "hello '${var1}')

var2 now contains    ->   hello 'mister'

So here I would conclude $(...) doesn't do the conversion either. DUHH!??!?

So it looks like there is some relationship between the combination $(...)
and the <<EOF construction

WHO CAN HELP????

 
 
 

command substitution

Post by Bill Marcu » Fri, 03 Oct 2003 03:43:24


On Wed, 1 Oct 2003 16:55:20 +0200, hpi


> If I combine this with the command substitution I can do

> var1=mister
> var2=$(cat <<EOF
> hello ${var1}
> EOF)

> var2 contains now: hello mister

> I also can use single quotes around the variable

> var1=mister
> cat <<EOF
> hello '${var1}'
> EOF

> result

> hello 'mister'

> NOW IT COMES

> if I want to put this in var2 using command substitution I use

> var1=mister
> var2=$( cat <<EOF
> hello '${var1}'
> EOF)

> var2 now contains !!!!!!!!!!   ->    hello "${var1}"

This is interesting.  What shell did you use?  Have you tried others?
(bash? zsh? pdksh? ksh88? ksh93? POSIX sh?)

--
Commander Spiral Pyjama Pseudo-Rhinocerous Feline Thingamajig Bill Marcum
(the First)
Ozy and Millie Name Generator http://heifong.phase.org/omname.php

 
 
 

command substitution

Post by Ben » Fri, 03 Oct 2003 04:47:12



>>NOW IT COMES

>>if I want to put this in var2 using command substitution I use

>>var1=mister
>>var2=$( cat <<EOF
>>hello '${var1}'
>>EOF)

>>var2 now contains !!!!!!!!!!   ->    hello "${var1}"

> This is interesting.  What shell did you use?  Have you tried others?
> (bash? zsh? pdksh? ksh88? ksh93? POSIX sh?)

I could reproduce this on HP-UX using ksh88.

regards,
Ben

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net

 
 
 

command substitution

Post by Ben » Fri, 03 Oct 2003 04:53:02



> NOW IT COMES

> if I want to put this in var2 using command substitution I use

> var1=mister
> var2=$( cat <<EOF
> hello '${var1}'
> EOF)

> var2 now contains !!!!!!!!!!   ->    hello "${var1}"

> Note the single quotes are converted into double quotes and the variable is
> not expanded.

Looks like a bug.

Ben

--
BTW. I can be contacted at Username:newsgroup4.replies.benaltw
Domain:xoxy.net

 
 
 

command substitution

Post by hpi » Fri, 03 Oct 2003 05:29:45



> On Wed, 1 Oct 2003 16:55:20 +0200, hpi

>> If I combine this with the command substitution I can do

>> var1=mister
>> var2=$(cat <<EOF
>> hello ${var1}
>> EOF)

>> var2 contains now: hello mister

>> I also can use single quotes around the variable

>> var1=mister
>> cat <<EOF
>> hello '${var1}'
>> EOF

>> result

>> hello 'mister'

>> NOW IT COMES

>> if I want to put this in var2 using command substitution I use

>> var1=mister
>> var2=$( cat <<EOF
>> hello '${var1}'
>> EOF)

>> var2 now contains !!!!!!!!!!   ->    hello "${var1}"

> This is interesting.  What shell did you use?  Have you tried others?
> (bash? zsh? pdksh? ksh88? ksh93? POSIX sh?)

I tried AIX 4.3 ksh and HP-UX 10.20 ksh and sh (POSIX.2)
They all have the same problem.

I also tried bash from cygwin but this one doesn't have this problem

 
 
 

command substitution

Post by hpi » Fri, 03 Oct 2003 05:34:55





>> NOW IT COMES

>> if I want to put this in var2 using command substitution I use

>> var1=mister
>> var2=$( cat <<EOF
>> hello '${var1}'
>> EOF)

>> var2 now contains !!!!!!!!!!   ->    hello "${var1}"

>> Note the single quotes are converted into double quotes and the
>> variable is not expanded.

> Looks like a bug.

> Ben

It does some parsing on the single quotes because if you leave out one of
the quotes you'll get an error : Syntax error: `'' is not matched

It looks like it tries to not expand the variable within single quotes.
This sounds logical except for the change to double quotes.

Also this only seems to go when the <<EOF construction is used

 
 
 

1. Command Substitution within Parameter Substitution?

Hi,

(GNU bash, version 2.05b.0(1)-release (i586-mandrake-linux-gnu).)

Is it possible, in the above version of BASH, to have (a) command
substitution(s) within (a) parameter substitution(s)?  I tried the
following in a BASH script:

gqview ${$( cat $HOME/mydisplay_return )#*\ }

and it didn't work.  The above code line was to execute GQview (an image
viewer) with the name of an image file that was stored, on it's own, in a
file called 'mydisplay_return', hence the 'cat' command to get at the image
file name.  The file name stored in the 'mydisplay_return' file was
prefixed with an identifying number followed by a space, hence the '#*\ '
parameter substitution to just leave the file name.  But it doesn't work.
What's wrong?

Yours,
Gary Hayward.

2. Pls point to docs on X-Windows 'interference'

3. Command substitution in rsh-command

4. xrdb dumps core

5. command substitution using sed

6. completely stumbled--hard disk not detected

7. nested command substitution in csh

8. Support for Always AL7000 SCSI card on Linux Universe 3.0

9. Problem: Command substitution adds an extra carriage return character on return.

10. Newbie question: Nested command substitution???

11. Multi-line command substitution (csh)

12. bash command substitution question

13. command substitution in sed