Nicosmik
Nicosmik

Reputation: 1131

How to write string with octal value

In bash, I would like to write the string "BLA\1" so it will be a buffer 42 4C 41 01 but the result is 42 4C 41 5C 31

To complete, in python if you write "BLA\1" in a binary file, the "\1" is interpreted as "1"

So how can I write the string "BLA\1" correctly in bash?

Upvotes: 1

Views: 260

Answers (2)

chepner
chepner

Reputation: 532323

Use printf, defined by the POSIX standard:

printf 'BLA\1'

Some bash-specific options:

# Let echo expand the escape code
echo -ne 'BLA\1'

# Use $'...', as in choroba's answer
echo -n $'BLA\1'

Upvotes: 0

choroba
choroba

Reputation: 242343

Use the $'' special quotes:

echo -n $'BLA\1' | xxd
00000000: 424c 4101                                BLA.

Upvotes: 3

Related Questions