쉘 스크립트에서는 here tag, here document에 설명되어 있다. 


여러 라인의 배시 커맨드를 간결하게 해준다고 보면 된다. 하지만 리터털이 아닌 표준입력으로 취급한다.




공식문서는 다음과 같다.


http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07_04


2.7.4 Here-Document

The redirection operators "<<" and "<<-" both allow redirection of subsequent lines read by the shell to the input of a command. The redirected lines are known as a "here-document".

The here-document shall be treated as a single word that begins after the next <newline> and continues until there is a line containing only the delimiter and a <newline>, with no <blank> characters in between. Then the next here-document starts, if there is one. The format is as follows:

[n]<<word
    here-document
delimiter

where the optional n represents the file descriptor number. If the number is omitted, the here-document refers to standard input (file descriptor 0). It is unspecified whether the file descriptor is opened as a regular file, a special file, or a pipe. Portable applications cannot rely on the file descriptor being seekable (see XSH lseek).

If any part of word is quoted, the delimiter shall be formed by performing quote removal on word, and the here-document lines shall not be expanded. Otherwise, the delimiter shall be the word itself.

If no part of word is quoted, all lines of the here-document shall be expanded for parameter expansion, command substitution, and arithmetic expansion. In this case, the <backslash> in the input behaves as the <backslash> inside double-quotes (see Double-Quotes). However, the double-quote character ( ' )' shall not be treated specially within a here-document, except when the double-quote appears within "$()""``", or "${}".

If the redirection operator is "<<-", all leading <tab> characters shall be stripped from input lines and the line containing the trailing delimiter. If more than one "<<" or "<<-" operator is specified on a line, the here-document associated with the first operator shall be supplied first by the application and shall be read first by the shell.

When a here-document is read from a terminal device and the shell is interactive, it shall write the contents of the variable PS2, processed as described in Shell Variables, to standard error before reading each line of input until the delimiter has been recognized.




here document에 대한 간단한 예시이다.  << 다음에 오는 구분자이기 때문에 마지막에 오는 구분자와 짝만 맞추면 된다. 관례상 EOF를 많이 쓰는 것 같다. 


cat << XXXX

> a="world"

> $a

> XXXX

a="world"

world



예시에서 here tag를 EOF로 가정하고 간단한 예시를 소개한다. 


리디렉션  <<을 사용해 한번에 배시를 처리할 수 있다. 



$ cat << EOF

> echo $a

> $a

> EOF

echo world

world



여러 라인으로 구성된 배시를 파일로 저장할 수 있다. 


$ cat << EOF > print_a.sh

> #!/bin/sh

> echo $a

> EOF

$ chmod 755 print_a.sh

$ ./print_a.sh

world



EOF처럼 EOS도 테스트할 수 있다.


$ cat << EOF

> h

> f

> p

> EOF

h

f

p




here tag 또는 here document는 표준 출력만 하기 때문에, 리터럴로 저장하려면, $ 또는 `를 사용해야 한다.

여러 라인으로 구성된 배시의 결과를 변수로 지정할 수 있다. 



$ multiline=$(cat << EOF

echo $a

$a

EOF

)


$ echo $multiline

echo world world




문서를 보면 expand라는 단어가 나오는데, 이 개념은 아래 예시를 통해 이해할 수 있다. << EOF를 사용하면 따옴표안의 변수 값이 변환되지만, <<\EOF를 사용하면 따옴표 안에 변수 값이 변환되지 않는다. 



$ cat << EOF

> a="world"

> printf '[%s]\n' "$a"

> EOF

a="world"

printf '[%s]\n' "world"



$ cat <<\EOF

> a="world"

> printf '[%s]\n' "$a"

> EOF

a="world"

printf '[%s]\n' "$a"




<<에 마이너스 기호(<<-)는 탭을 무시하겠다는 의미를 가진다. 






Posted by '김용환'
,