User Tools

Site Tools


quickreference:unix

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
quickreference:unix [2024/03/04 15:54] rodolicoquickreference:unix [2025/02/05 00:12] (current) – [Shell (mainly BASH)] rodolico
Line 237: Line 237:
 ===== Shell (mainly BASH) ===== ===== Shell (mainly BASH) =====
  
 +==== Here Documents ====
 +
 +Most unix users are familiar with echo'ing something to STDOUT so it can be used as STDIN for a following script (using the pipe). However, this is usually limited to a single line.
 +
 +A **here document** is a way of having multiple lines processed at one time. In many cases, you can have similar functionality using quotes, but here documents are more robust.
 +
 +For example, a simple test of a newly built mail system might include creating a file with all of the headers necessary, then passing that to //sendmail// for processing. However, you can skip a step and simply send the message directly using a here document.
 +
 +<code bash>
 +sendmail user@example.com << EOF
 +To: user@example.com
 +from: root@example.org
 +Subject: test
 +
 +This is a test
 +EOF
 +</code>
 +
 +The entire block above is one command. Here is the breakdown.
 +
 +  - //sendmail user@example.com// is a standard sendmail invocation which assumes the message itself is coming from STDIN.
 +  - //<<// marks the beginning of a //here document//
 +  - //EOF// is the tag which will mark the end of the text for the here document
 +  - Everything up to the EOF is the actual string to be passed to sendmail
 +  - //EOF// at the end marks the end of the here document. **Note**: there must be no leading or trailing whitespace. The tag must be exactly as entered after the << (case sensitive), and must be the only thing on the final line.
 +
 +This only touches the surface of here documents. See [[https://tldp.org/LDP/abs/html/here-docs.html]] for a lot more information.
 ==== Find files within date range containing text ==== ==== Find files within date range containing text ====
  
quickreference/unix.1709589254.txt.gz · Last modified: 2024/03/04 15:54 by rodolico