I have binary file that I want to replace with shell script 0xaa,0xbb,0xcc,0xdd with 0x11,0x22,0x33,0x44

I can get 0xaa,0xbb,0xcc,0xdd into shell script like $1 but how can I replace it with 0x11,0x22,0x33,0x44 without sed?


  • You might want to clarify your requirements. For example, sed is a standard part of Ubuntu and, for that matter, all POSIX systems. How is it that you have an Ubuntu system that doesn't have sed? Answering that question might help us understand what tools you do have available. – John1024 Feb 3 '19 at 21:46
  • Perhaps it's a requirement of the script and not that OPs lacks the sed tool! – George Udosen Feb 3 '19 at 22:29
  • 1
    Probably tr is more appropriate than (line-base) sed anyhow - another option would be perl (which has its own tr and may be invoked in "slurp mode") – steeldriver Feb 3 '19 at 22:37

Perhaps bash indexed arrays would help:

#!/usr/bin/env bash
set -e

my_hex=(0xaa 0xbb 0xcc 0xdd)
my_new_hex=(0x11 0x22 0x33 0x44)

# exchange values
for i in $(seq 1 "${#my_hex[@]}")
        my_hex["$i" - 1]=${my_new_hex[$i - 1]}
        echo "${my_hex[$i - 1]}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.