tags:

views:

97

answers:

3

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
+3  A: 

For me, I type ctrl-V followed by ctrl-I to insert a tab in the bash shell. This gets around the shell intercepting the tab, which otherwise has a 'special' meaning. Ctrl-V followed by a tab should work too.

When embedding dollar signs in a here document you need to disable interpolation of shell variables, or else prefix each one with a backslash to escape (i.e. \$).

Using your example text I ended up with this content in prices.txt:

coffee\t.50
tea\t.50
burger\t.00

because $1 and $5 are not set. Interpolation can be switched off by quoting the terminator, for example:

cat > prices.txt <<"EOF"
martin clayton
+1 Thank you for your answer. What if I wanted to store the here doc in a file to run later? For example, what if I wanted to use a here-doc to provide an example file I could when running a paticular script and store the here-doc commented in the same file as the script for later reference?
D W
@D W - I'm not sure I follow what you're asking there - you can certainly embed here docs in scripts.
martin clayton
Thank you for mentioning the solution to including dollar signs in here-docs without having them interpreted.
D W
+2  A: 

You can embed your here doc in your script and assign it to a variable without using a separate file at all:

#!/bin/bash
read -r -d '' var<<"EOF"
coffee\t$1.50
tea\t$1.50
burger\t$5.00
EOF

Then printf or echo -e will expand the \t characters into tabs. You can output it to a file:

printf "%s\n" "$var" > prices.txt

Or assign the variable's value to itself using printf -v:

printf -v var "%s\n" "$var"

Now var or the file prices.txt contains actual tabs instead of \t.

You could process your here doc as it's read instead of storing it in a variable or writing it to a file:

while read -r item price
do
    printf "The price of %s is %s.\n" $item $price    # as a sentence
    printf "%s\t%s\n" $item $price                  # as a tab-delimited line
done <<- "EOF"
    coffee $1.50    # I'm using spaces between fields in this case
    tea $1.50
    burger $5.00
    EOF

Note that I used <<- for the here doc operator in this case. This allows me to indent the lines of the here doc for readability. The indentation must consist of tabs only (no spaces).

Dennis Williamson
+1 I learned the most from this answer and it solves all my problems with here-docs an some ways to use them effectively.
D W
+2  A: 
TAB="$(printf '\t')"

cat > prices.txt << EOF
coffee${TAB}\$1.50
tea${TAB}\$1.50
burger${TAB}\$5.00
EOF
chuck
+1 Thank you for mentioning an easy solution to including tabs in here-docs.
D W
A bash-specific shortcut would be `TAB=$'\t'`, abusing the gettext features.
Daenyth
@Daenyth, Thanks for the tip, that's a more elegant way.
D W