3

I'd like to know what the minus (-) and the EOC in the command below means. I know some languages like Perl allows you to chose any combination of character (not bound to EOF) but is that the case here? And the minus is a complete mystery for me. Thanks in advance!

ftp -v -n $SERVER  >> $LOG_FILE <<-EOC
            user $USERNAME $PWD
            binary
            cd $DIR1
            mkdir $dir_lock
            get $FILE
            bye
EOC
Lee
  • 43

2 Answers2

7

That's a here-document redirection.

command <<-word
here-document contents
word

The word used to delimit the here-document is arbitrary. It's common, but not necessary, to use an upper-case word.

The - in <<-word has the effect that tabs will be stripped from the beginning of each line in the contents of the here-document.

cat <<-SERVICE_ANNOUNCEMENT
    hello
    world
SERVICE_ANNOUNCEMENT

If the above here-document was written with literal tabs at the start of each line, it would result in the output

hello
world

rather than

    hello
    world

Tabs before the end delimiter are also stripped out with <<- (but not without the -):

cat <<-SERVICE_ANNOUNCEMENT
    hello
    world
    SERVICE_ANNOUNCEMENT

(same output)

Kusalananda
  • 333,661
1

From man bash:

   If the redirection operator is <<-, then all leading tab characters are
   stripped  from  input  lines  and  the line containing delimiter.  This
   allows here-documents within shell scripts to be indented in a  natural
   fashion.
  • Stephen Harris, thank you! I can't believe I didn't look for it in the man page. I just skimmed through EOF and EOC and missed <<- Thank you, again! – Lee Apr 23 '17 at 18:08