Bash-Script

printf 輸出執行到下一行

  • April 14, 2014

我在vi. 我的輸出執行到它下面的行中。我是 UNIX 新手,所以我正在努力學習。我的輸出使用printf.

printf "%-15s %15s %15s %2d\n %2s " $name $days $phone $start $time

例如,輸出看起來像這樣

name       days       phone      start 

time name    days       phone      start

time name    days       phone      start 

etc...

如何讓我的所有五個變數都列印在同一行上?

你的命令:

printf "%-15s %15s %15s %2d\n %2s " $name $days $phone $start $time

你的問題:

'...\n %2s'

您在之前插入換行符$time。停下。做:

printf '%-15s %15s %15s %2d %2s\n' \
   "$name" "$days" "$phone" "$start" "$time"

除了@mikeserv 的回答,您還可以查看以下輸出的格式控制項的完整列表man 1 printf

  \"     double quote    
  \\     backslash    
  \a     alert (BEL)    
  \b     backspace    
  \c     produce no further output    
  \e     escape    
  \f     form feed    
  \n     new line    
  \r     carriage return    
  \t     horizontal tab    
  \v     vertical tab    
  \NNN   byte with octal value NNN (1 to 3 digits)    
  \xHH   byte with hexadecimal value HH (1 to 2 digits)    
  \uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits)    

  \UHHHHHHHH
         Unicode character with hex value HHHHHHHH (8 digits)    

  %%     a single %    
  %b     ARGUMENT as a string with `\' escapes interpreted, except that 
         octal escapes are of the form \0 or \0NNN

您應該閱讀man <command name>以了解如何在任何*nix作業系統中使用命令。

引用自:https://unix.stackexchange.com/questions/124610