2016-08-05 148 views

回答

6

man bash,如果搜索<<-(通過鍵入:/<<-Enter),你會發現:

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. 

例如:

$ cat << EOF 
>  hello 
> there 
> EOF 
    hello 
there 

同樣的事情,但用<<-代替<<

$ cat <<- EOF 
>  hello 
> there 
> EOF 
hello 
there 

「hello」行上的前導TAB字符被剝離。

由於從man頁面報價說, 這是shell腳本非常有用,例如:

if cond; then 
    cat <<- EOF 
    hello 
    there 
    EOF 
fi 

這是習慣縮進代碼塊中的線路在此if聲明,爲了更好的可讀性。 沒有<<-運算符的語法,我們將被迫寫上面的代碼是這樣的:

if cond; then 
    cat << EOF 
hello 
there 
EOF 
fi 

這是非常不愉快的閱讀,它在更復雜的現實腳本變得更糟。

但請記住,作爲@glenn-jackman指出:

請注意,只有製表符被刪除,不是任意的空白。請小心,您的文本編輯器不會將製表符轉換爲空格。

+0

請注意,只有*製表符*被刪除,而不是任意的空格。請小心,您的文本編輯器不會將製表符轉換爲空格。 –

+0

@glennjackman,你是對的,這是很好的指出,添加到我的文章,謝謝! – janos

+1

這就是爲什麼我不使用它們:我不信任別人(或我自己)來維護選項卡字符。我生活在醜陋之中。 –