string2octal.sh 233 B

12345678910111213
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. string="$*"
  4. output=""
  5. for (( i=0; i<${#string}; i++ )); do
  6. char=${string:$i:1}
  7. octal=$(printf '%03o' "'$char")
  8. output="$output\\$octal"
  9. done
  10. echo "\$'${output}'"