\n in variable in heredoc

前端 未结 5 1480
一个人的身影
一个人的身影 2020-12-10 04:57

Is there any way to for a Bash heredoc to interpret \'\\n\\\' in a heredoc?

I have an iteratively built string in a loop, something like

for i in wor         


        
5条回答
  •  一向
    一向 (楼主)
    2020-12-10 05:51

    As others (and other answers to other questions) have said, you can put encoded characters into a string for the shell to interpret.

    x=$'\n' # newline
    printf -v x '\n' # newline
    

    That said, I don't believe there is any way to directly put an encoded newline into a heredoc.

    cat <

    just outputs a literal \n

    cat <<$'EOF'
    …
    EOF
    

    is nothing special, nor is <<'EOF'

    The best you can do is to preencode the newline, and include the expansion in the heredoc:

    nl=$'\n'
    cat <

    outputs

    foo bar
     baz
    

提交回复
热议问题