Reputation: 3713
I'm using sed
to replace every instance of " is "
by a colorized "'s "
.
To do this, it surrounds the result by a color variable r and a blank variable x, which are interpreted literally. echo -e
then makes the colors appear. Nothing new here.
The problem arises when I try to add the apostrophe (which is actually a single quote); I can't escape it without messing up the color escape code.
Here is my code without the apostrophe I'm trying to add:
r='\e[0;31m' # red
x='\e[0m' # reset color
sed 's/ is /\'${r}'s\'${x}'/g' # replace " is " by "'s "
I tried double quotes, multiple backslashes, but everything either broke the color code, or failed to escape the single quote. In the meantime, I'm using a true apostrophe ( ’ ), but it it's not the best solution, because it doesn't render properly in the TTY.
Upvotes: 0
Views: 477
Reputation: 328754
If you want to keep the single quotes, then you need '"'"'
The first quote ends the current single quoted string. Then we have "'"
which expands to a single quote. \'
would also work but doesn't look as good.
The last single quote starts the quoted string again.
An alternative in your case would be "s/ is /${r}'s${x}/g"
(i.e. only use double quotes).
But then, you need to use r='\\e[0;31m'
because the expansion in the string will string one level of escapes.
Upvotes: 1