Using a long string as input with here-documents

In some cases, you may have a long multi-line string for input to a command in your script that you don't want to separate into another file. This is often the case with output for -h or --help options, where you don't want to store the help information outside the script.

Because quoted strings can include multiple lines, we can just use printf for this; it works fine, emitting multiple lines of output:

#!/bin/bash
case $1 in
    -h|--help)
        printf '%s\n' 'foo command help:
-h, --help: Show this help
-q, --quiet: Run without diagnostics
-v, --verbose: Add extra diagnostics'
        exit 0
        ;;
esac

For commands that accept the data as input, such as cat, rather than in argument, such as printf, a different ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.