我想知道下面命令中的減號 (-) 和 EOC 是什麼意思。我知道像 Perl 這樣的一些語言允許您選擇任意字元組合(不綁定到 EOF),但這裡是這種情況嗎?負號對我來說完全是個謎。先致謝!

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

答案1

這是此處文檔重定向。

command <<-word
here-document contents
word

用於word分隔此處文件的 是任意的。使用大寫單字很常見,但不是必需的。

in-<<-word作用是從此處文件內容的每行開頭刪除製表符。

cat <<-SERVICE_ANNOUNCEMENT
    hello
    world
SERVICE_ANNOUNCEMENT

如果上面的此處文件在每行開頭都使用文字製表符編寫,則將導致輸出

hello
world

而不是

    hello
    world

結束分隔符號之前的製表符也會被刪除<<-(但不能沒有-):

cat <<-SERVICE_ANNOUNCEMENT
    hello
    world
    SERVICE_ANNOUNCEMENT

(相同的輸出)

答案2

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.

相關內容