首页 文章

bash中的Printf示例不会创建换行符

提问于
浏览
19

在bash脚本中使用 printf ,在 "\n" 之后不添加空格不会创建换行符,而添加空格会创建换行符,例如: G . :

  • "\n" 之后没空位
NewLine=`printf "\n"`
echo -e "Firstline${NewLine}Lastline"

结果:

FirstlineLastline
  • "\n " 之后的空间
NewLine=`printf "\n "`
echo -e "Firstline${NewLine}Lastline"

结果:

Firstline
 Lastline

问题:为什么不1.创建以下结果:

Firstline 
Lastline

我知道这个特定的问题本来可以使用其他技术,但我想关注为什么1.不起作用 .

编辑:当使用echo而不是printf时,我得到了预期的结果,但为什么printf的工作方式不同?

NewLine=`echo "\n"`
    echo -e "Firstline${NewLine}Lastline"

结果:

Firstline
    Lastline

7 回答

  • 1

    反引号运算符删除尾随的新行 . 见 3.4.5. Command substitution at http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html

    关于编辑问题的注释

    相比:

    [alvaro@localhost ~]$ printf "\n"
    
    [alvaro@localhost ~]$ echo "\n"
    \n
    [alvaro@localhost ~]$ echo -e "\n"
    
    
    [alvaro@localhost ~]$
    

    echo命令不会将 \n 视为换行符,除非您告诉他这样做:

    NAME
           echo - display a line of text
    [...]
           -e     enable interpretation of backslash escapes
    

    POSIX 7指定此行为here

    [...]使用命令的标准输出,在替换结束时删除一个或多个字符的序列

  • -2

    看起来BASH正在删除尾随换行符 . 例如

    NewLine=`printf " \n\n\n"`
    echo -e "Firstline${NewLine}Lastline"
    Firstline Lastline
    
    NewLine=`printf " \n\n\n "`
    echo -e "Firstline${NewLine}Lastline"
    Firstline
    
    
     Lastline
    
  • 3

    您编辑的 echo 版本将一个字面反斜杠-n放入变量 $NewLine ,然后由 echo -e 解释 . 如果你这样做了:

    NewLine=$(echo -e "\n")
    echo -e "Firstline${NewLine}Lastline"
    

    你的结果与第一种情况相同 . 为了使那个工作方式,你必须逃避反斜杠并将整个事情放在单引号中:

    NewLine=$(printf '\\n')
    echo -e "Firstline${NewLine}Lastline"
    

    或双重逃脱:

    NewLine=$(printf "\\\n")
    

    当然,您可以直接使用 printf ,也可以像这样设置NewLine值:

    printf "Firstline\nLastline\n"
    

    要么

    NewLine=$'\n'
    echo "Firstline${NewLine}Lastline"    # no need for -e
    
  • 1
    $ printf -v NewLine "\n"
    $ echo -e "Firstline${NewLine}Lastline"
    
    Firstline
    Lastline
    
    $ echo "Firstline${NewLine}Lastline"
    Firstline
    Lastline
    
  • 3

    我们不需要“echo”或“printf”来创建NewLine变量:

    NewLine="
    "
    printf "%q\n" "${NewLine}"
    echo "Firstline${NewLine}Lastline"
    
  • 3

    也许人们会带着同样的问题来到这里:在包裹在后台的代码中回显\ n . 小提示:

    printf "astring\n"
    # and 
    printf "%s\n" "astring" 
    # both have the same effect.
    # So... I prefer the less typing one
    

    简短的回答是:

    # Escape \n correctly !
    
    # Using just: printf "$myvar\n" causes this effect inside the backsticks:
    printf "banana
    "
    
    # So... you must try \\n  that will give you the desired 
    printf "banana\n"
    
    # Or even \\\\n if this string is being send to another place 
    # before echoing,
    
    buffer="${buffer}\\\\n printf \"$othervar\\\\n\""
    

    一个常见的问题是,如果你在代码里面做:

    echo 'Tomato is nice'
    

    当用后挡板包围时会产生错误

    command Tomato not found.
    

    解决方法是添加另一个echo -e或printf

    printed=0
    
    function mecho(){
      #First time you need an "echo" in order bash relaxes.
      if [[ $printed == 0 ]]; then
        printf "echo -e $1\\\\n"
        printed=1
      else
        echo -e "\r\n\r$1\\\\n"
      fi
    }
    

    现在您可以在提示中调试您的代码:

    (prompt)$  `mySuperFunction "arg1" "etc"`
    

    输出会很好

    mydebug: a value
     otherdebug: whathever appended using myecho
     a third string
    

    并在内部进行调整

    mecho "a string to be hacktyped"
    
  • 24

    如果你添加“\ r”,工作正常

    $ nl=`printf "\n\r"` && echo "1${nl}2"
    1
    2
    

相关问题