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

Re: bash quoting problems

Andreas Berglund wrote:
> I have a problem with the following sed snippet
> sed -i s"|^\( *PATH="\)\(.*\)|\1$ADD:\2|" ~/profile-test
> I need soft quotes in order for $ADD to expand and I also need to
> math against one doublequote in the regexp in for $ADD to be put in
> the corrct place. Does anyone know how to do this?

You will need to quote double-quotes within double-quotes.

  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.

  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.

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

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


Attachment: signature.asc
Description: Digital signature

Reply to: