I want to change my PS1 in my .bashrc file. I've found a script using printf with %q directive to escape characters :
#!/bin/bash
STR=$(printf "%q" "PS1=\u@\h:\w\$ ")
sed -i '/PS1/c\'"$STR" ~/.bashrc
The problem is that I get this error :
script.sh: 2: printf: %q: invalid directive
Any idea ? Maybe an other way to escape the characters ?
The printf
command is built into bash. It's also an external command, typically installed in /usr/bin/printf
. On most Linux systems, /usr/bin/printf
is the GNU coreutils implementation.
Older releases of the GNU coreutils printf
command do not support the %q
format specifier; it was introduced in version 8.25, released 2016-10-20. bash's built-in printf
command does -- and has as long as bash has had a built-in printf
command.
The error message implies that you're running script.sh
using something other than bash.
Since the #!/bin/bash
line appears to be correct, you're probably doing one of the following:
sh script.sh
. script.sh
source script.sh
Instead, just execute it directly (after making sure it has execute permission, using chmod +x
if needed):
./script.sh
Or you could just edit your .bashrc
file manually. The script, if executed correctly, will add this line to your .bashrc
:
PS1=\\u@\\h:\\w\$\
(The space at the end of that line is significant.) Or you can do it more simply like this:
PS1='\u@\h:\w\$ '
One problem with the script is that it will replace every line that mentions PS1
. If you just set it once and otherwise don't refer to it, that's fine, but if you have something like:
if [ ... ] ; then
PS1=this
else
PS1=that
fi
then the script will thoroughly mess that up. It's just a bit too clever.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With