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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
That’s a here-document.
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)
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0