General
cancel
Showing results for 
Search instead for 
Did you mean: 

Command substitution strange behavior?

Jesper Sivertsen
Frequent Advisor

Command substitution strange behavior?

Hi there
I have seen a strange behavior i HP-UX that sombody have to explain to mee??

If you try this scipt:
---------------------------------
RES=$(cat <single 'qoute'
double "qoute"
single \'qoute\'
END
)
echo $RES
RES=`cat <single 'qoute'
double "qoute"
single \'qoute\'
END
`
echo $RES
---------------------------------

In HP-UX the result is:
single "qoute" double "qoute" single \'qoute\'
single 'qoute' double "qoute" single \'qoute\'


But in linux bash it is ( as exspected) :
single 'qoute' double "qoute" single \'qoute\'
single 'qoute' double "qoute" single \'qoute\'

Why is there a diffrense in HP-UX between $() and `` ?

Regards Jesper
All in unix is files
8 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: Command substitution strange behavior?

Hi Jesper:

First, the HP-UX Posix shell ('/usr/bin/sh' or '/sbin/sh') does differ from the 'bash' shell.

I can't offer a reason for the difference you see, however.

It is interesting, though, that if you do:

#!/usr/bin/sh
cat <single 'qoute' double "qoute" single \'qoute\'
END
cat <single 'qoute' double "qoute" single \'qoute\'
END

...you get the expected:

single 'qoute' double "qoute" single \'qoute\'
single 'qoute' double "qoute" single \'qoute\'

...
I'm not sure why you are resorting to collecting a here-doc in a variable and then echoing that variable.

Regards!

...JRF...
OldSchool
Honored Contributor

Re: Command substitution strange behavior?

For what it may be worth,in ksh on AIX, the result is identical to the HP-UX output the OP posted.

Jesper Sivertsen
Frequent Advisor

Re: Command substitution strange behavior?

This is just an example to show the "problem".
I use the construction to run sqlplus from oracle and get the result in a variable.

Jesper
All in unix is files
Bill Hassell
Honored Contributor

Re: Command substitution strange behavior?

Just to clarify: HP-UX doesn't know anything about quotes and strings and echo. HP-UX is an OS just like Linux. What you are describing is specific to shell programs, and just like Linux and AIX, HP-UX has several shells. As mentioned, the typical shell for HP-UX is the POSIX shell: /usr/bin/sh and not to be confused with the Bourne shell: /usr/old/bin/sh. But you may have been given the Korn shell (aka, ksh-88) for your login on HP-UX, or maybe an optional shell called Bash.

The $() construct is always preferred for modern shells as it can provide unambiguous nesting and is infinitely easier to document properly. The reason is that ' and ` are very, very often typed incorrectly. Read the man page for sh-posix (HP-UX), ksh (any OS) and Bash (any OS) to see the deprecation (obsolete) of grave accents for command substitution.

Bottom line, there are differences between shells. I don't have a clear explanation for the changing of single quotes into double quotes. But as you might imagine, characters that special meaning to the shell will always need extra handling.


Bill Hassell, sysadmin
Raj D.
Honored Contributor

Re: Command substitution strange behavior?

Jesper,

Check this out , similar bash WT... page:
you will find more interesting stuff,

http://almirkaric.com/tag/awk/

Hth,


" If u think u can , If u think u cannot , - You are always Right . "
Jesper Sivertsen
Frequent Advisor

Re: Command substitution strange behavior?

I said HP-UX because i se the problem in both posix-shell and ksh.

I know all about shells.
I have worked vith HP-UX and other unixes for more than 15 years and been teathing HP-UX at HP.

That is why i do not understand the difference.

Regards Jesper
All in unix is files
Dennis Handly
Acclaimed Contributor

Re: Command substitution strange behavior?

>I have seen a strange behavior I HP-UX that somebody have to explain to me?

It looks like a bug with new-fangled $() and here documents. Please report it to the Response Center.

RES=$(cat <
It fails even with: <<\END
If you do this, you see the here documented is messed up when it parses the $():
RES=$(cat <<\END > /dev/tty
single 'qoute'
double "qoute"
single \'qoute\'
END
)

>JRF: I can't offer a reason for the difference you see

It's broken. :-(

>It is interesting, though, that if you do:

That doesn't mix the gas with a match.

>Bill: characters that special meaning to the shell will always need extra handling.

And it failed.
Duncan Edmonstone
Honored Contributor

Re: Command substitution strange behavior?

It's interesting that both sh and ksh are ksh88 type shells, whereas dtksh which is a ksh93 type shell works as expected...

# more ./de.sh
#!/usr/dt/bin/dtksh
RES=$(cat <single 'qoute'
double "qoute"
single \'qoute\'
END
)
echo $RES
RES=`cat <single 'qoute'
double "qoute"
single \'qoute\'
END
`
echo $RES

# ./de.sh
single 'qoute' double "qoute" single \'qoute\'
single 'qoute' double "qoute" single \'qoute\'


Wonder if this is some obscure feature - the POSIX/IEEE standards are almost impossible for joe-admins like me to make sense of, but I wonder if there is something in there that might explain this behaviour...

HTH

Duncan

HTH

Duncan