This fails since printf
interprets some backslashed letters in its first argument as special.
In general when using printf
you supply
- A static formatting string, possibly containing placeholders for data that should be taken from the other arguments, and
- Other variable data to be used in the placeholders in the static formatting string.
This means that the first argument to printf
can always be a single quoted string.
In the shell, you may use just %s
as a placeholder for virtually anything in the formatting string, unless you want things like left/right padding (e.g. %20s
to use 20 characters for a right-justified string, or %-20s
for a left-justified string), a certain number of decimal places in floats (%.3f
for three decimal places), or zero-filling of integers (%08d
for eight digits, zero-filled) etc.
For example, to print your string, use %s
in the formatting string:
v='\u\$'
printf 'PS1 will be set to "%s"\n' "$v" # or just: printf '%s\n' "$v"
PS1="$v"
or
PS1='\u\$'
printf 'PS1 was set to "%s"\n' "$PS1" # or just: printf '%s\n' "$PS1"
This will output
PS1 will be set to "\u\$"
and
PS1 was set to "\u\$"
See printf(1)
and/or printf(3)
on your system (man 1 printf
and man 3 printf
).
Note, too, that PS1
will not need to be exported.