diff --git a/ebook/en/content/023-bash-redirection.md b/ebook/en/content/023-bash-redirection.md index 82c80c4..cb6cb7e 100644 --- a/ebook/en/content/023-bash-redirection.md +++ b/ebook/en/content/023-bash-redirection.md @@ -172,6 +172,49 @@ Output: 18 ryan ``` +# HereDocument + +The symbol `<<` can be used to create a temporary file [heredoc] and redirect from it at the command line. +``` +COMMAND << EOF + ContentOfDocument + ... + ... +EOF +``` + +Note here that `EOF` represents the delimiter (end of file) of the heredoc. In fact, we use any alphanumeric word in it's place to signify the start and end of the file. For instance, this is a valid heredoc: +``` +cat << randomword1 + This script will print these lines on the terminal. + Note that cat only accepts a filename as it's argument. Using this heredoc, + we can create a temporary file with these lines as it's content and pass + that file as the argument to cat. +randomword1 +``` + +Effectively it will appear as if the contents of the heredoc are piped into the command. This can make the script very clean if multiple lines need to be piped into a program. + +Further, we can attach more pipes as shown: +``` +cat << randomword1 | wc + This script will print these lines on the terminal. + Note that cat only accepts a filename as it's argument. Using this heredoc, + we can create a temporary file with these lines as it's content and pass + that file as the argument to cat. +randomword1 +``` + +Also, pre-defined variables can be used inside the documents. + +# HereString + +Herestrings are quite similar to heredocs but use `<<<`. These are used for single line strings that have to be piped into some program. This looks cleaner that heredocs as we don't have to specify the delimiter. + +``` +cat <<<"this is an easy way of passing strings to the stdin of a program (here wc)" | wc +``` + ## Summary |**Operator** |**Description** | |:---|:---| @@ -180,3 +223,5 @@ Output: |`<`|`Read input from a file`| |`2>`|`Redirect error messages`| |`\|`|`Send the output from one program as input to another program`| +|`<<`|`Pipe multiple lines into a program cleanly`| +|`<<<`|`Pipe a single line into a program cleanly`|