Bash/Printf

Aus SchnallIchNet
Wechseln zu: Navigation, Suche

string escaping

to 'bash'-escape a string use printf

a=''\''"\;:#[]{}()|&^$@!?, .<>abc123'
printf -v var "%q" "$a"
echo "$var"
\'\"\\\;:#\[\]\{\}\(\)\|\&\^\$@\!\?\,\ .\<\>abc123

have fun!


repeated output

if you want to format output e.g. by spaces do

printf "%10s" " "

this will produce a 10 bytes long string of ' ' (spaces).
if you want to have anything else than spaces use tr

printf "%10s" " " | tr ' ' '#'
##########

this is because non-given strings are interpretes as NULL-string
so the following does NOT work:

printf "%10s" "#"
         #

printf expects a 10-byte long string because of the %10s format string.
because it only gets 1 byte. it prints out 9 bytes spaces and only ONE '#'
this is the reason you will have to use tr