In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to use the printf command of Linux. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
The common Linux command printf command imitates the printf () function in C language. The main function is to output text, according to the format we specify.
Printf formats and outputs the results.
Directory bash built-in command
Commands in GNU coreutils
Built-in command summary printf [- v var] format [arguments] main purpose formatting parameters and output.
Option-v var: output the result to the variable var instead of to standard output. Parameter format: output format.
Arguments: one or more parameters.
Escape sequences: in addition to supporting printf (1) and printf (3) escape sequences, the built-in printf also supports the following escape sequences: backslash escape characters in the% b expansion parameter. % Q expands the parameter to use as shell input. % (fmt) T outputs a date-time string based on the escape characters in strftime (3). The return status is successful unless an illegal option, write error, or assignment error is given.
Example #%-5s format is left-aligned and width 5 string instead ('- 'means left-aligned), if not used, right-aligned by default. The #%-4.2f format has a left-aligned width of 4, retaining two decimal places. Printf "%-5s%-10s%-4s\ n" NO Name Mark printf "%-5s%-10s%-4.2f\ n" 01 Tom 90.3456 printf "%-5s%-10s%-4.2f\ n" 02 Jack 89.2345 printf "%-5s%-10s%-4.2f\ n" 03 Jeff 98.4323 # output NO Name Mark 01 Tom 90.35 02 Jack 89.23 03 Jeff 98.43 #% b% Q% (fmt) T example # see it again with a newline. Printf "% s\ n" 'hello world' # expands the newline character, as shown above. Printf "% b" 'hello world\ n' printf'% Q\ n' a b c'# output a\ b\ c #% z is the time zone and% n is the newline character. Printf "% (% F% T% z% n) T" # output 2019-09-10 01:48:07 + 0000 Note this command is a bash built-in command, please see the help command for help.
External command summary printf FORMAT [ARGUMENT]. The main purpose of printf OPTION is to format parameters and output.
Option-help displays help and exits. -- version displays version information and exits. Parameter format: output format.
Arguments: one or more parameters.
Omitted here (% b% Q), if the version of coreutils you installed supports them, please refer to the example above. Supported escape sequence:\ "double quotation marks\\ backslash\ a bell\ b backspace\ c truncated output\ e exit\ f page flip\ nnewline\ r return\ t horizontal tab\ v Vertical tabs\ NNN octal numbers (1 to 3 digits)\ xHH hexadecimal numbers (1 to 2 digits)\ uHHHH Unicode characters with 4 hexadecimal digits\ UHHHHHHHH Unicode characters with 8 hexadecimal digits%% sign and C format specification for an ending in 'diouxXfeEgGcs' Will be converted to the correct type and handle variable width. Example # use / usr/bin/printf to ensure that you are not calling a built-in command. # of course, if you close the built-in printf and confirm that there is no printf function in the current environment, you can use printf directly, as shown in the link "Note" at the end. # print the subscripts and values of arrays and associative arrays by row. # declare an array without adding 'declare-a' or 'local-a' (local variables declared within the function). Arr= ('line1'' line2') / usr/bin/printf "% s\ n" ${! arr [@]} # output subscript 0 1 / usr/bin/printf "% s\ n" ${arr [@]} # output value line1 line2 # declares that associative arrays (that is, dictionaries) must add 'declare-A' or 'local-A' (local variables declared in the function). Declare-An assoc_arr= (['key1'] =' value1' ['key2'] =' value2') / usr/bin/printf "% s\ n" ${! assoc_arr [@]} # output key. Key2 key1 / usr/bin/printf "% s\ n" ${assoc_arr [@]} # output value. The return status of value2 value1 is successful unless an illegal option is given.
Note that this command is a command in the GNU coreutils package. For help, please see man-s 1 printf or info coreutils' pwd invocation'.
To start or close the built-in command, please see the enable command, and for the priority of the same name, see the discussion in the examples section of the builtin command.
Through my communication with bug-bash@gnu.org, I got an explanation of these format specifiers% b% Q% (fmt) T:
The% b format specifier in printf (1) is an additional POSIX feature in addition to the formats supported by printf (3).
The% Q and% T specifiers are non-standard and are not supported by all independently implemented printf.
These are all the contents of this article entitled "how to use Linux's printf commands". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.