On-line Guides

How To Guides






Advanced Bash-Scripting Guide:
Prev Chapter 17. Here DocumentsNext

17.1. Here Strings

A here string can be considered as a stripped-down form of here document. It consists of nothing more than COMMAND <<<$WORD, where $WORD is expanded and fed to the stdin of COMMAND.

Example 17-13. Prepending a line to a file

#!/bin/bash
# prepend.sh: Add text at beginning of file.
#
# Example contributed by Kenny Stauffer,
#+ and slightly modified by document author.
E_NOSUCHFILE=65
read -p "File: " file # -p arg to 'read' displays prompt.
if [ ! -e "$file" ]
then # Bail out if no such file.
 echo "File $file not found."
 exit $E_NOSUCHFILE
fi
read -p "Title: " title
cat - $file <<<$title > $file.new
echo "Modified file is $file.new"
exit 0
# from 'man bash':
# Here Strings
# 	A variant of here documents, the format is:
# 
# 		<<<word
# 
# 	The word is expanded and supplied to the command on its standard input.

Exercise: Find other uses for here strings.


Prev Home Next
Here DocumentsUp Recess Time
Published under the terms of the GNU General Public License Design by Interspire

AltStyle によって変換されたページ (->オリジナル) /