5

I was asked the following question in a test on shell scripting at my university, which never gave an answer, and google is of little help.

Q: What is the line separator that should be used to end this here-document?

fff=file
xyz <<\\$fff
...
7
  • 2
    Pedagogically, why would you put a question like that on a test? It's a corner case at best, probably a worst practice, and easily and habitually avoided by every "here document" writer ever. Sometimes you've gotta wonder about academics...
    – user732
    May 21, 2015 at 13:03
  • @Bruce: Pedagogically, because many students practice on and study from the many old exams that are readily available, and many students (and woefully many teachers) take a pattern recognition / memorization approach to learning (does anybody remember “flash cards”?) that leads to “educated” people knowing the answers to a large number of questions, and having some ability to adapt the answers to similar questions (e.g., if the answer to <<EOF is EOF, then the answer to <<foo must be foo) without really having the ability to answer questions or knowing the underlying theory.  … C’d May 21, 2015 at 22:37
  • (Cont’d) …  So a teacher might strive to ask a question that isn’t trivially isomorphic to last year’s question (e.g., changing <<foo to <<bar is trivial) and that does challenge their understanding of the underlying rules and mechanisms.  That said, I agree with you; for a person to be able to answer that in a closed-book exam, he would have had to memorize every word of bash(1).  I personally, would have guessed that <<$fff would have a terminator of file.  … (Cont’d) May 21, 2015 at 22:37
  • (Cont’d) …  This relates to a pet peeve of mine.  I don’t know what the problem is — it seems like some teachers have such a poor grasp of the theory themselves, that they compensate by teaching a million specific examples instead of teaching the theory.  I applaud test questions like “What is the library function that writes number to the standard output in a human-readable way?” and “What is the printf conversion type (i.e., the letter after the %) for printing floating point numbers?”.  I disapprove of questions like  … (Cont’d) May 21, 2015 at 22:37
  • (Cont’d) …  “What is the exact, complete printf conversion specification to print a floating-point number with five character positions to the left of the decimal point (including - sign if appropriate, and leading spaces if necessary) and two digits to the right of the decimal point?” (unless it’s an open-book test).  I believe that there’s no benefit to memorizing the printf specs to that level of detail.  The first couple of times you need to print a floating-point number, you’ll check the man page.  If it’s something that you do often enough, you’ll learn it through repetition. May 21, 2015 at 22:38

2 Answers 2

9

The line which ends the here document is

\$fff

From the man bash section on Here Documents:

The format of here-documents is:

          <<[-]word
                  here-document
          delimiter

No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \newline is ignored, and \ must be used to quote the characters \, $, and `.

word does undergo quote removal, so \\$fff is dequoted to \$fff. But, as the manpage says, no variable expansion is done so it stays that way.

The body of a here document might or might not undergo variable expansion and backslash interpretation. In this case, since word contains a quoted character (that is, the backslash), parameter expansion and backslash dequoting are not performed on the text of the here document.

However, the input is compared with the terminating sequence before variable expansion, so it is not necessary to backslash-escape the \ nor the $ in the terminating line.

3
  • typo? - \$file ?
    – Peter.O
    May 21, 2015 at 10:29
  • Re your last sentence, "As a result, it is not ...". Your preceding sentence refers to the body of the here document, not the terminator. The terminator - called the delimiter in man bash - is, as per your quote from man bash, "the delimiter is the result of quote removal on word"
    – Peter.O
    May 21, 2015 at 10:54
  • @Peter.O: Correct on both counts. Only backslash-newline escape processing is performed prior to comparing the delimiter (and in this case, that's irrelevant because the quoted backslash inhibits escape processing).
    – rici
    May 21, 2015 at 14:12
4

The terminator is

\$fff

I'm going to assume that that's what @rici meant to say.  As his penultimate paragraph says,

word does undergo quote removal, so \\$fff is dequoted to \$fff. But, as the man page says, no variable expansion is done so it stays that way.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .