简体   繁体   中英

bash script echo always print the same space

I have a simple question. it's a bit hard to explain but I will do my best. I have multiple scripts that prints numbers, for example temperature and memory usage. these numbers can be 4 numbers (1000 MB) or less (500 MB), it needs to echo ( 500 MB). I know how to use the same space by making it 0500 MB, but thats really ugly. You can probably do this with printf, but i have no idea how.

73nismit

printf "%7s" "500 MB"

将添加一个前导空格,使字符串占据 7 个字符。

printf "#%4i#\n" 500

gives # 500#

you should look at the section 'Field and printing modifiers' in the reference manual

It's basically the same for all printf implementations.

printf <FORMAT> <ARGUMENTS...>

You neded sth. like

printf "%7s\n" "500MB"

Have a look here for more examples:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM