Echo multiline string into file bash

MarksCode

I want to echo some text that has newlines into a file in my bash script. I could just do echo -e "line1 \n line2 \n" but that would become unreadable when the text becomes very long. I tried doing the following instead with line breaks:

echo -e "[general]\
    state_file = /var/lib/awslogs/agent-state\
    [/var/log/messages]\
    file = /var/log/messages\
    log_group_name = /var/log/messages\
    log_stream_name = {ip_address}\
    datetime_format = %b %d %H:%M:%S\

However, while the text was inserted, no newlines were placed so the whole thing was on one line. Is there anyway to echo text with newlines while also making the bash script readable?

Tom Fenech

If you want to use echo, just do this:

echo '[general]
state_file = /var/lib/awslogs/agent-state
[/var/log/messages]
file = /var/log/messages
log_group_name = /var/log/messages
log_stream_name = {ip_address}
datetime_format = %b %d %H:%M:%S'

i.e. wrap the whole string in quotes and don't try and escape line breaks. It doesn't look like you want to expand any shell parameters in the string, so use single quotes.

Alternatively it's quite common to use a heredoc for this purpose:

cat <<EOF
[general]
state_file = /var/lib/awslogs/agent-state
[/var/log/messages]
file = /var/log/messages
log_group_name = /var/log/messages
log_stream_name = {ip_address}
datetime_format = %b %d %H:%M:%S
EOF

Note that shell parameters will be expanded in this case. Using bash, you can use <<'EOF' instead of <<EOF on the first line to avoid this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to write a multiline string to a file in Bash

From Dev

How to convert multiline file into a string in bash with newline character?

From Dev

appensinding multiline variable to file bash

From Dev

Replace string with multiline file content

From Dev

Search for multiline String in a text file

From Dev

Replace string with multiline file content

From Dev

Replace text in file with multiline string

From Dev

echo special characters to a file in bash

From Dev

How to echo counter to file in bash

From Dev

How to echo counter to file in bash

From Dev

bash echo into another file not working

From Dev

bash replace multiline text in file, with pattern

From Dev

bash loop file echo to each file in the directory

From Dev

multiline regex match string replacement on large file

From Dev

multiline regex match string replacement on large file

From Dev

Replace a multiline string with the content of a file.txt

From Java

How to echo a variable following a string in bash?

From Java

parse and echo string in a bash while loop

From Dev

Another Bash string escape question (using echo)

From Dev

Bash - Echo is splitting my string result

From Dev

Bash script to read from a file and echo

From Dev

In bash script, is it possible to echo something into the beginning of file?

From Dev

Elegant solution to echo to either stdout or file in bash

From Dev

Adding java output to echo string of bat file

From Dev

How do I create a multiline text file with Echo in Windows command prompt?

From Dev

Groovy replacing place holders of file content similar to multiline-string

From Dev

Render markdown from a yaml multiline string in a Jekyll data file

From Dev

Save a multiline string to a text file without overwriting previous text

From Dev

Groovy replacing place holders of file content similar to multiline-string

Related Related

  1. 1

    How to write a multiline string to a file in Bash

  2. 2

    How to convert multiline file into a string in bash with newline character?

  3. 3

    appensinding multiline variable to file bash

  4. 4

    Replace string with multiline file content

  5. 5

    Search for multiline String in a text file

  6. 6

    Replace string with multiline file content

  7. 7

    Replace text in file with multiline string

  8. 8

    echo special characters to a file in bash

  9. 9

    How to echo counter to file in bash

  10. 10

    How to echo counter to file in bash

  11. 11

    bash echo into another file not working

  12. 12

    bash replace multiline text in file, with pattern

  13. 13

    bash loop file echo to each file in the directory

  14. 14

    multiline regex match string replacement on large file

  15. 15

    multiline regex match string replacement on large file

  16. 16

    Replace a multiline string with the content of a file.txt

  17. 17

    How to echo a variable following a string in bash?

  18. 18

    parse and echo string in a bash while loop

  19. 19

    Another Bash string escape question (using echo)

  20. 20

    Bash - Echo is splitting my string result

  21. 21

    Bash script to read from a file and echo

  22. 22

    In bash script, is it possible to echo something into the beginning of file?

  23. 23

    Elegant solution to echo to either stdout or file in bash

  24. 24

    Adding java output to echo string of bat file

  25. 25

    How do I create a multiline text file with Echo in Windows command prompt?

  26. 26

    Groovy replacing place holders of file content similar to multiline-string

  27. 27

    Render markdown from a yaml multiline string in a Jekyll data file

  28. 28

    Save a multiline string to a text file without overwriting previous text

  29. 29

    Groovy replacing place holders of file content similar to multiline-string

HotTag

Archive