1

I'm trying to both printf and export the variable used for setting PS1 but everything I've tried returns error in printf or puts literal \u in prompt.

v='\u\$'; printf "$v"; export PS1="$v"
-bash: printf: missing unicode digit for \u

v='\\u\$'; printf "$v"; export PS1="$v"
\u$

v=$'\u'; v+='\$'; printf "${v}"; export PS1="${v}"
-bash: printf: missing unicode digit for \u

Is there a way to format $v to make both printf and export work?

8

This fails since printf interprets some backslashed letters in its first argument as special.

In general when using printf you supply

  1. A static formatting string, possibly containing placeholders for data that should be taken from the other arguments, and
  2. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.