Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Difference between $( <command> ) and `<command>`

 
SOLVED
Go to solution
Occasional Advisor

Difference between $( <command> ) and `<command>`

Hi all,

Since a POSIX course at HP I learned to write proper POSIX, so I always use $( ).
But today I tried to fill a variable with an SQL*PLUS query and came up with an issue.
With the following example I will try to explain the problem:

$export SQLDBA='sqlplus -s'
$echo $( ${SQLDBA} <<-EOF
/ as sysdba
set head off
select value from v\\$nls_parameters where parameter = 'NLS_CHARACTERSET';
exit;
EOF )
sh: nls_parameters: Parameter not set.

echo ` ${SQLDBA} <<-EOF
/ as sysdba
set head off
select value from v\\$nls_parameters where parameter = 'NLS_CHARACTERSET';
exit;
EOF `
AL32UTF8

I thought it wouldn't matter which method to use. Did I just find a crop circle?

I prefer to use the $( ) method, so can anyone explain this behaviour and tell me how to use $( )? In fact I want to fill a variable, so in my script the “echo” will be replaced by “VAR=”

Thanks in ad
15 REPLIES 15
Highlighted
Acclaimed Contributor

Re: Difference between $( <command> ) and `<command>`

I see tons of errors in your first example:
$export --> export
$echo ---> echo
heredocs <<-EOF but terminated by EOF
An extra backslash before $


This should be much closer to functional code:

export SQLDBA='sqlplus -s'
echo $( ${SQLDBA} <<-EOF
/ as sysdba
set head off;
select value from v\$nls_parameters where parameter = 'NLS_CHARACTERSET';
exit;
-EOF
)
If it ain't broke, I can fix that.
Highlighted
Trusted Contributor
Solution

Re: Difference between $( <command> ) and `<command>`

I'd say those extra $ are probably just command line prompts and for a heredoc
if <<- is used, leading tabs are ignored

as it looks like they are running this from the command line it would be interesting to know if the results are the same from a file.
Highlighted
Honored Contributor

Re: Difference between $( <command> ) and `<command>`

Hi Paul,
I cant find my docs...but your issue is with encapsulated variables (variable of (variable...))
not very posix...
I remembered going through such cases and the way out is using eval...
now an elegant way would be:
alias sq='sqlplus -s'
export SQLDBA='sqlplus -s'
echo $( ${SQLDBA} <<-EOF
/ as sysdba
set head off;
select value from v\$nls_parameters where parameter = 'NLS_CHARACTERSET';
exit;
-EOF
)


All the best
Victor
Highlighted
Honored Contributor

Re: Difference between $( <command> ) and `<command>`

Sorry
alias sq='sqlplus -s'

echo $( ${sq} <<-EOF
/ as sysdba
set head off;
select value from v\$nls_parameters where parameter = 'NLS_CHARACTERSET';
exit;
-EOF
)

Was what I wanted...

All the best
Victor
Highlighted
Acclaimed Contributor

Re: Difference between $( <command> ) and `<command>`

You are quite correct about the <<-EOF although my "standard" heredocs marker is always !EOF! because it stands out so well and there is no possible confusion of dashes and tildes on small fonts.

If it ain't broke, I can fix that.
Highlighted
Honored Contributor

Re: Difference between $( <command> ) and `<command>`

The grave accent marks `` are deprecated (not recommended) in all the POSIX shell man pages (sh-posix, ksh, etc). $(...) are strongly recommended for two reasons:

1. `` is much too easily confused with '' so copying a script (by someone unfamiliar with the proper usage of grave accent marks) usually has a lot of errors, and

2. grave accent marks cannot be nested so you can't use the result of one process (command substitution) as a parameter to another process which is itself being used as a command substitution.


Bill Hassell, sysadmin
Highlighted
Occasional Advisor

Re: Difference between $( <command> ) and `<command>`

Hi guys,

Thanks all for the quick responses. I'm sorry for the leading '$'. That was only the prompt copied and pasted. Maybe that wasn't really handy...
The extra minus sign in my heredoc was indeed to ignore leading tabs.
The code is in a function in a script and will be used by another function (which is still in the pen).
If executed from a script (with the braces), an "l" (like ls with IFS=\040) of the current working is given, together with an ORA-902 ERROR.

Like I asked before: Why does the POSIX method act differently (thus incorrect) than the Bourne method while using the identical syntax??

The issue isn't really the heredocs or the aliases, but the fact that using a dollar {$} and quotes {'} make that POSIX doesn't work and Bourne is successful?

The actual code is:
# make this one global, it will be used more often
export SQLDBA='sqlplus -s'

function f_get_charset
{
CHARSET=$(
${SQLDBA} <<-EOF
/ as sysdba
set head off
select value from v\\$nls_parameters where parameter = 'NLS_CHARACTERSET';
exit;
EOF
)
# End of f_get_charset
}

Any suggestions? I really want to use POSIX, because I like (good) standards and don't want to let my scripts getting messy.

Thanks in advance!
Highlighted
Trusted Contributor

Re: Difference between $( <command> ) and `<command>`

the $() method you need to use v\$nls_parameters (only one backslash)

the `` method you need to use v\\$nls_parameters (two backslashes)

why?
from the man page:
Command Substitution
The standard output from a command enclosed in parenthesis preceded by a dollar sign ($(...)) or a pair of grave accents (`...`) can be used as part or all of a word; trailing newlines are removed. In the second (archaic) form, the string between the accents is processed for special quoting characters before the command is executed. See the "Quoting" subsection.

Inside grave accent marks (`...`), \ quotes the characters \, `, and $.


as an example try this

echo "$(cat <<-ABC
from v\$nls_parameters
ABC
)"
echo `cat <<-ABC
from v\\$nls_parameters
ABC
`



Highlighted
Trusted Contributor

Re: Difference between $( <command> ) and `<command>`

Hi Paul,

I Tried with a "set -x" at the first line of the function (That's debug mode).
When I try your syntax, I found out that the single qoutes are changed into double quotes.
Look in the man-pages in the section "Quoting" for a solution.

Cheers,

Rene
Nothing is more successfull as failure