How do I pass hex characters to printf in a script command?

How do I get printf to output hex characters when run from a script?

Suppose I am at the prompt and type printf "x41x42" the output I get

 AB%

However if I have a script containing that line i.e. cat test.sh produces

printf "x41x42"

But executing the script ./test.sh produces

x41x42%

How do I get the script to produce the same results as from the shell prompt?

Asked By: sgmoore

||

Try using the external printf utility:

/usr/bin/printf "x41x42"

You have probably have a bash shell but as your interactive shell, while sh is linked to dash.

The dash built-in printf doesn’t know xNN, so you have to use octal values, NN, instead.

The bash built-in printf does know xNN

Answered By: Schmaehgrunza
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.