Bash/Printf: Unterschied zwischen den Versionen
Aus SchnallIchNet
< Bash
Cbs (Diskussion | Beiträge) K (hat „Printf“ nach „Bash/Printf“ verschoben) |
Cbs (Diskussion | Beiträge) |
||
Zeile 1: | Zeile 1: | ||
− | ==repeated output== | + | == string escaping == |
+ | |||
+ | to 'bash'-escape a string use printf | ||
+ | |||
+ | <pre> | ||
+ | a=''\''"\;:#[]{}()|&^$@!?, .<>abc123' | ||
+ | printf -v var "%q" "$a" | ||
+ | echo "$var" | ||
+ | \'\"\\\;:#\[\]\{\}\(\)\|\&\^\$@\!\?\,\ .\<\>abc123 | ||
+ | </pre> | ||
+ | have fun! | ||
+ | |||
+ | |||
+ | == repeated output == | ||
+ | |||
if you want to format output e.g. by spaces do | if you want to format output e.g. by spaces do | ||
printf "%10s" " " | printf "%10s" " " |
Aktuelle Version vom 25. April 2013, 08:44 Uhr
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