Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have been given an example program, I am wondering what exactly the <<ENDOFMENU and ENDOFMENU does, won't it work the same if you leave it out and just use the while loop?

#!/bin/sh
echo "Menu test program...";
stop=0;
while test $stop -eq 0; do
cat<<ENDOFMENU
1: print the date
2,3 : print the current working directory
4: exit
ENDOFMENU
  echo; echo -e "your choice?\c"
  read reply
echo
case $reply in
    "1")
       date
;;
    "2"|"3")
       pwd
;;
    "4")
      stop=1
;;
    *)
      echo illegal choice
  esac
done
share|improve this question

closed as off topic by karthick87, Ringtail, Mitch, jokerdino, Jorge Castro Sep 17 '12 at 14:34

Questions on Ask Ubuntu are expected to relate to Ubuntu within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
This question may be better located at stackoverflow.com . –  Christopher Kyle Horton Sep 17 '12 at 11:56

1 Answer 1

up vote 3 down vote accepted

ENDOFMENU as used in your example is a so-called here document or here doc. It allows for a multi-line string without needing to escape quoting characters such as ' and ".

Quoting the bash(1) manual:

This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.

The format of here-documents is:

<<[-]word
here-document
delimiter

No parameter 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. In the latter case, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and `.

You showed the following code in your example:

cat<<ENDOFMENU
1: print the date
2,3 : print the current working directory
4: exit
ENDOFMENU
  echo; echo -e "your choice?\c"

That makes a string available as stream and then prints them to the console using the cat command. Finally, it prints another blank line followed by the string your choice? and an escape sequence that means "produce no further output" and effectively strips the newline that would follow. It could be rewritten as:

echo -e "    1: print the date
2,3 : print the current working directory
4: exit

your choice?\c"
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.