Here Document in Shell

Level 4108 keystrokes

A here document passes multi-line text to a command. With the unquoted delimiter EOF, ${USER} is expanded and the text is written to config.txt; with the quoted 'LITERAL' the text is used as is, so only "$USER stays literal" is printed.

Shell code

# Unquoted EOF: ${USER} is replaced by the user name
# > sends the three lines to config.txt instead of the screen
1cat <<EOF > config.txt
2host=localhost
3port=8080
4user=${USER}
5EOF
# Quoted delimiter: nothing is expanded, so cat prints "$USER stays literal" as written
6cat <<'LITERAL'
7$USER stays literal
8LITERAL

Output

$USER stays literal
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Shell code and how often they appear.

  • <× 4
  • =× 3
  • '× 2
  • $× 2
  • .× 1
  • {× 1
  • }× 1
  • >× 1

More Shell examples at level 4

← Shell typing practice