How do you type a tab in a bash here-document?

Viewed 21312

The definition of a here-document is here: http://en.wikipedia.org/wiki/Here_document

How can you type a tab in a here-document? Such as this:

cat > prices.txt << EOF
coffee\t$1.50
tea\t$1.50
burger\t$5.00
EOF

UPDATE:

Issues dealt with in this question:

  1. Expanding the tab character
  2. While not expanding the dollar sign
  3. Embedding a here-doc in a file such as a script
12 Answers

If you want to use tabs for the file indentation and for the heredoc: You just need to separate the tabs of the indentation, from the tabs of the document with a whitespace:

try_me() {
    # @LinuxGuru's snippet; thanks!
    sed 's/^ //g' >> tmp.conf <<-EOF
     /var/log/nginx/*log { 
        daily
        rotate 10
        missingok
        notifempty
        compress
        sharedscripts
        postrotate
            /bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || :
        endscript
     }
    EOF
}

try_me

The only drawback is that the not-indented lines will look a little weird; they have a leading whitespace char on the script

  • /var/log/nginx/*log
  • }

However, that won't be there on the resulting file (sed 's/^ //g' instead of cat)

Use @EOF and it will preserve tabs.

cat >> /etc/logrotate.d/nginx <<@EOF
/var/log/nginx/*log { 
    daily
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || :
    endscript
}
@EOF
Related