bash metachars in pattern substitution

104 Views Asked by At

I usually use the bash pattern substitution ${parameter/pattern/string} and I wonder if there are any substitution string metcharacters like & in other tools (for instance sed, awk).

I'm trying to use the bash pattern substitution to insert 0x just before each pair of hexadecimal digits: 1a0e22fe ---> 0x1a 0x0e 0x22 0xfe

If & worked like in sed and awk, it would be so easy:

S=1a0e22ff
echo "${S//??/ 0x&}"

Are there metachars in substitution string?

1

There are 1 best solutions below

0
On

I'm afraid no. This is too complex for Bash. Using a tool like sed instead is better.

input | sed 's/../0x&/g' | output