Randy McMurchy wrote:
> Bruce Dubbs wrote these words on 02/28/06 19:12 CST:
> 
>> You have me interested.  Can you give an example?
> 
> Yes I can.
> 
> However, you must concede that if you agree there is difference in
> behavior (one works, one does not), then it is worth switching. Okay,
> the shell syntax is complicated, but I found a need to have to do it.
> 
> I will explain as I go.
> 
> First set a string variable
> 
> BIGSTRING="this is the string to test for"
> 
> Now I want to pick a word out of that string using sed. That word is
> "string" and I must identify it by a variable.
> 
> MYSTRING="string"
> 
> Notice this is one word out of $BIGSTRING
> 
> We are in a loop right now with a variable being a counter of the
> loop. Each repetition through the loop increments the counter.
> 
> COUNTER=1
> 
> Now I echo the string, sedding each line and if it is a match, then
> I want to make a replacement based on the number of the counter.
> 
> So, the command is as follows (I won't insult you by explaining the sed):
> 
> echo ${BIGSTRING} | sed "s/^.*\($MYSTRING\).*$/\\${COUNTER}/"
> 
> The word "string" was returned, as expected.
> 
> Now, we need to make the same expression so that it is assigned to
> a variable. Here is where we get into trouble.
> 
> First using `...` syntax.
> 
> JUSTFORBRUCE=`echo ${BIGSTRING} | sed "s/^.*\($MYSTRING\).*$/\\${COUNTER}/"`
> 
> We have assigned a variable:
> 
> echo $JUSTFORBRUCE
> 
> Woops, we didn't get what we wanted. We got ${COUNTER} instead
> 
> Now issue the *same exact command* using $(...) syntax:
> 
> JUSTFORBRUCE=$(echo ${BIGSTRING} | sed "s/^.*\($MYSTRING\).*$/\\${COUNTER}/")
> 
> Notice that now the desired string was returned: "string".
> 
> Need I say more?  :-)

No. But the behavior is documented in the bash man page:

"When the old-style backquote form of substitution is used, backslash
retains its literal meaning except when followed by $, `, or \.  The
first backquote not preceded by a backslash terminates  the  command
substitution.   When  using  the  $(command)  form,  all  characters
between the parentheses make up the command; none are  treated  specially."

So the backslash is treated differently in the different constructs.  I
like to learn new things so today is a success.  :)

I still have no problems with using backquoted form if the command has
been tested.  AFAIK it is not depreciated.

I also note, not that it means much, that the backquote form is easier
to type for lazy typists.  :) The backquote is 2 keystrokes.  The $()
for is six, counting the shift key three times.

  -- Bruce

-- 
http://linuxfromscratch.org/mailman/listinfo/blfs-dev
FAQ: http://www.linuxfromscratch.org/blfs/faq.html
Unsubscribe: See the above information page

Reply via email to