- Bash Quick Start Guide
- Tom Ryder
- 328字
- 2021-07-23 16:58:59
The printf command
The printf command works like echo, except the first argument you provide to it is a format string:
$ printf '%s\n' 'Hello!' Hello!
Notice that we had to put the format string in single quotes, to prevent the backslash from having special meaning to the shell. Notice also that we didn't have to use double quotes to get the newline from \n; the printf command did that for us.
You can use printf in much the same way you would use a printf() function in other languages, such as C. It supports most of the same format specifications as the printf(1) system command; you can type man 1 printf to see a list.
It's easier to print tricky strings with printf, where echo might struggle:
$ printf '%s\n' -n -n $ string=-n $ printf '%s\n' "$string" -n
You should always choose printf over echo in scripts, even though it's a little more typing.
printf also has a useful property where it will repeat the format string as necessary for each argument you provide it. This is a convenient way to print a list of arguments on individual lines:
$ printf '%s\n' foo bar baz foo bar baz
Note that we got three instances of the string-newline pattern from one format string.
Finally, Bash's printf has a %q pattern that can be used to quote special characters in a string with backslashes, so it can be reused safely in the shell. If you follow good quoting practices, you are unlikely to need this a lot in Bash, but it's useful to know it's there:
bash$ printf '%q\n' 'Watch out for thi$ $tring; it \has\ nasty character$!' Watch\ out\ for\ thi\$\ \$tring\;\ it\ \\has\\\ nasty\ character\$\!
- Getting Started with MariaDB
- 腦動(dòng)力:PHP函數(shù)速查效率手冊(cè)
- INSTANT Varnish Cache How-to
- CorelDRAW X4中文版平面設(shè)計(jì)50例
- 樂高創(chuàng)意機(jī)器人教程(中級(jí) 下冊(cè) 10~16歲) (青少年iCAN+創(chuàng)新創(chuàng)意實(shí)踐指導(dǎo)叢書)
- 大數(shù)據(jù)時(shí)代
- Google SketchUp for Game Design:Beginner's Guide
- 邊緣智能:關(guān)鍵技術(shù)與落地實(shí)踐
- Applied Data Visualization with R and ggplot2
- Dreamweaver CS6中文版多功能教材
- Flink原理與實(shí)踐
- Mastering Ceph
- 基于Proteus的單片機(jī)應(yīng)用技術(shù)
- 數(shù)據(jù)要素:全球經(jīng)濟(jì)社會(huì)發(fā)展的新動(dòng)力
- 空間機(jī)器人智能感知技術(shù)