/bin/sh would be dash what is the correct syntax for printf to display certain ascii character using hex or dex code in dash? let's say i want to printf a dollar sign ($). which hex or dec code should be used and how?
Asked
Active
Viewed 2,669 times
-4
1 Answers
5
If you want to go from the character code to the character itself, you include the character code in the printf
format string, escaped with a backslash, in octal.
E.g. printf "\044\n"
prints $
(and a newline).
In Bash and other shells, you could use hex, \x24
, but that's not standard and doesn't work in Dash.
You could nest another printf
in a command substitution to convert from hex or decimal to octal, though. Both of these would print $
(and a newline):
printf "\\$(printf %o 36)\n"
printf "\\$(printf %o 0x24)\n"

ilkkachu
- 138,973
-
-
Note that
$
is only octal 44, hex 24 when expressed in the ASCII charset (and all its supersets such as iso8859-15, gb18030 or UTF-8).printf '\44\n'
will print a$
on a large majority of POSIX systems, but certainly not all of them, and POSIX for example does not guarantee that it does.printf '\u0024'
wherever supported should print a$
though as that's the U+0024 character, even where its encoding is not the 0x44 byte. So wouldprintf '\44' | iconv -f ASCII
(more portable). – Stéphane Chazelas Jun 08 '21 at 21:37
dash
? – steeldriver Jun 08 '21 at 20:23