1

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?

Jdamian
  • 3,015
  • 2
  • 17
  • 22
  • Did any of the answers below work for you. Do provide feedback so that we can help you better. – Inian Dec 20 '17 at 15:59
  • Both answers work, but I already use in my scripts and that is not I was looking for. What I needed is that somebody confirm there is no bash trick to do the same. – Jdamian Dec 20 '17 at 21:14

1 Answers1

1

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

input | sed 's/../0x&/g' | output
iBug
  • 35,554
  • 7
  • 89
  • 134