[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash quoting problems



On 2011-08-09 01:51, Bob Proulx wrote:

   sed -i s"|^\( *PATH=\"\)\(.*\)|\1$ADD:\2|" ~/profile-test

The above seems like you are doing more than you need with the long
match for (.*) and \2.  It isn't needed.  It is much more efficient to
avoid matching a potentially infinite amount of data.

Thanks for the tip, I just assumed sed does greedy matching.

   sed -i "s|^\( *PATH=\"\)|\1$ADD:|" ~/profile-test

Sometimes quoting can be complicated.  In which case it is often
useful to drop out of double quotes and move to single quotes.

   sed -i "s|^\( *PATH="'"'"\)|\1$ADD:|" ~/profile-test

That "'"'" looks scary but isn't.  ..."  '"'  "...  One double quote
to stop the double quoted string.  One single, one double, one single,
to insert one double quote.  Then another double quote to pick up the
double quoted string.

Thanks, I saw a recommendation to that effect when googling but didn't get how it worked until now.


Or reverse it for the variable.  Which is probably what I would do
there.

   sed -i 's|^\( *PATH="\)|\1'"$ADD"':|' ~/profile-test

Good thinking I'll keep that in mind :D

Bob


Reply to: