By Alvin Alexander. Last updated: June 4, 2016
I was just working on my Linux Teleport command (Linux cd command with a history), and ran across the code below, which essentially shows how to use a form of "heredoc" syntax in a Bash shell script. This approach uses the Linux cat command, but functions just like the heredoc syntax in languages like Perl.
Here's the source code I just came across:
function displayTpUsageStatement() { cat <<'EOFFOE' Usage: tp [DIR] Purpose: Provide the same capabilities as the 'cd' command, but with a memory of directories that you have previously visited. -h Show HELP (this output) -j PARTIAL-DIR JUMP to a directory found in your history (by partial directory name) -l LIST the history of visited directories -n [NUM] Go to a directory by NUMBER. NUM corresponds to the number printed by the -l option. If NUM is not specified then a list of visited directories is shown and user can pick a number. -s DIR SEARCH for and display a directory named DIR found in your history EOFFOE }
In this example, everything between this line:
cat <<'EOFFOE'
and this ending line:
EOFFOE
are echoed to the display when this function is called. As you can probably tell from the name of the function, this echoes a usage statement to the user of this command.
If you need to use a 'heredoc' in a Linux shell script (Bourne shell, or Bash script), I hope this example is helpful.