Skip to main content
added 281 characters in body
Source Link
Lesmana
  • 28.2k
  • 20
  • 85
  • 87

There are two problems in your echo command.

I expanded the variables for brevity.

$ echo -e "test TEST test" | sed -e "s/TEST/\e[1;37mTEST\e[0m/g"
test e[1;37mTESTe[0m test

First problem: the backslashes are lost somewhere between sed magic and shell magic. You have to escape them.

$ echo -e "test TEST test" | sed -e "s/TEST/\\e[1;37mTEST\\e[0m/g"
test e[1;37mTESTe[0m test

Still doesn't work. Maybe more escaping?

$ echo -e "test TEST test" | sed -e "s/TEST/\\\e[1;37mTEST\\\e[0m/g"
test \e[1;37mTEST\e[0m test

Finaly the backslashes are coming through. I have no idea why youwe need three backslashes, that is some weird sed magic.

Second problem: the echo -e is pointless. -e enables interpretation of backslash escapes, but all echo -e gets to interpret is "test TEST test". The backslash escapes are coming from sed, which does not care about them and prints them raw.

What you want is this:

$ echo -e $(echo "test TEST test" | sed -e "s/TEST/\\\e[1;37mTEST\\\e[0m/g")
test TEST test

with bold TEST in output.

Here is the full script with the changes:

#!/bin/bash

ColorOff='\\\e[0m'       # Text Reset
BWhite='\\\e[1;37m'      # Bold White

string="test TEST test"
echo -e $(echo "$string" | sed -e "s/TEST/${BWhite}TEST${ColorOff}/g")

There are two problems in your echo command.

I expanded the variables for brevity

$ echo -e "test TEST test" | sed -e "s/TEST/\e[1;37mTEST\e[0m/g"
test e[1;37mTESTe[0m test

First problem: the backslashes are lost somewhere between sed magic and shell magic. You have to escape them.

$ echo -e "test TEST test" | sed -e "s/TEST/\\e[1;37mTEST\\e[0m/g"
test e[1;37mTESTe[0m test

Still doesn't work. Maybe more escaping?

$ echo -e "test TEST test" | sed -e "s/TEST/\\\e[1;37mTEST\\\e[0m/g"
test \e[1;37mTEST\e[0m test

Finaly the backslashes are coming through. I have no idea why you need three backslashes, that is some weird sed magic.

Second problem: the echo -e is pointless. -e enables interpretation of backslash escapes, but all echo -e gets to interpret is "test TEST test". The backslash escapes are coming from sed, which does not care about them and prints them raw.

What you want is this:

$ echo -e $(echo "test TEST test" | sed -e "s/TEST/\\\e[1;37mTEST\\\e[0m/g")
test TEST test

with bold TEST.

There are two problems in your echo command.

I expanded the variables for brevity.

$ echo -e "test TEST test" | sed -e "s/TEST/\e[1;37mTEST\e[0m/g"
test e[1;37mTESTe[0m test

First problem: the backslashes are lost somewhere between sed magic and shell magic. You have to escape them.

$ echo -e "test TEST test" | sed -e "s/TEST/\\e[1;37mTEST\\e[0m/g"
test e[1;37mTESTe[0m test

Still doesn't work. Maybe more escaping?

$ echo -e "test TEST test" | sed -e "s/TEST/\\\e[1;37mTEST\\\e[0m/g"
test \e[1;37mTEST\e[0m test

Finaly the backslashes are coming through. I have no idea why we need three backslashes, that is some weird sed magic.

Second problem: the echo -e is pointless. -e enables interpretation of backslash escapes, but all echo -e gets to interpret is "test TEST test". The backslash escapes are coming from sed, which does not care about them and prints them raw.

What you want is this:

$ echo -e $(echo "test TEST test" | sed -e "s/TEST/\\\e[1;37mTEST\\\e[0m/g")
test TEST test

with bold TEST in output.

Here is the full script with the changes:

#!/bin/bash

ColorOff='\\\e[0m'       # Text Reset
BWhite='\\\e[1;37m'      # Bold White

string="test TEST test"
echo -e $(echo "$string" | sed -e "s/TEST/${BWhite}TEST${ColorOff}/g")
Source Link
Lesmana
  • 28.2k
  • 20
  • 85
  • 87

There are two problems in your echo command.

I expanded the variables for brevity

$ echo -e "test TEST test" | sed -e "s/TEST/\e[1;37mTEST\e[0m/g"
test e[1;37mTESTe[0m test

First problem: the backslashes are lost somewhere between sed magic and shell magic. You have to escape them.

$ echo -e "test TEST test" | sed -e "s/TEST/\\e[1;37mTEST\\e[0m/g"
test e[1;37mTESTe[0m test

Still doesn't work. Maybe more escaping?

$ echo -e "test TEST test" | sed -e "s/TEST/\\\e[1;37mTEST\\\e[0m/g"
test \e[1;37mTEST\e[0m test

Finaly the backslashes are coming through. I have no idea why you need three backslashes, that is some weird sed magic.

Second problem: the echo -e is pointless. -e enables interpretation of backslash escapes, but all echo -e gets to interpret is "test TEST test". The backslash escapes are coming from sed, which does not care about them and prints them raw.

What you want is this:

$ echo -e $(echo "test TEST test" | sed -e "s/TEST/\\\e[1;37mTEST\\\e[0m/g")
test TEST test

with bold TEST.