0

I saw the following command:

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xxxxxxxx.mirror.aliyuncs.com"]
}
EOF

What is the hyphen (‐) used for? It works fine even i remove the hyphen.

Kusalananda
  • 333,661
  • It will cause it to strip leading tabs (and only tabs, not spaces!) from each line from the here-document, and also recognize the delimiter (EOF) if preceded by tabs. –  Jul 15 '19 at 08:00

1 Answers1

4

The hyphen/dash is used for telling the shell to remove any leading tab character from the here-document. This is documented by POSIX as part of the here-document redirection:

If the redirection operator is <<-, all leading <tab> characters shall be stripped from input lines and the line containing the trailing delimiter. [...]

The feature allows for creating slightly prettier scripts:

while some-condition; do

    some-command <<-END_INPUT
    some data
    goes here
    END_INPUT

done

(where each line in the here-document and the line containing the END_INPUT delimiter are indented using tabs), as opposed to

while some-condition; do

    some-command <<END_INPUT
some data
goes here
END_INPUT

done
Kusalananda
  • 333,661