Bash 'here document' word / delimiter not quotation marks

I have the following script:

#!/bin/bash
cat << EOF
^A^B^C
EOF

Where:

  • '^ A' = decimal value 1
  • '^ B' = decimal value 2
  • '^ C' = decimal value 3

So the file looks like this:

xxd main.sh 
00000000: 2321 2f62 696e 2f62 6173 680a 6361 7420  #!/bin/bash.cat 
00000010: 3c3c 2045 4f46 0a01 0203 0a45 4f46 0a    << EOF.....EOF.

When I run the script, the decimal value 1 disappears:

./main.sh | xxd
00000000: 0203 0a                                  ...

I have to quote the word / separator ( cat << "EOF") for the decimal value 1 so that it also displays, but I don't understand why.

In the bash reference manual, this happens when it is wordnot displayed:

If the word without quotes, all lines of the document here undergo parameter expansion, command substitution and arithmetic expansion, the \ newline character sequence is ignored, and '\ should be used to quote the characters' \, $ and ".

My question is:

1 , / ? , ?

+4
1

, , .

Bash , (istring). :

#0  expand_word_internal (word=0x7fffffffd610, quoted=2, isexp=0, contains_dollar_at=0x0, expanded_something=0x0) at subst.c:9155
#1  0x00005555555c1bb9 in call_expand_word_internal (w=0x7fffffffd610, q=2, i=0, c=0x0, e=0x0) at subst.c:3614
#2  0x00005555555c1cb3 in expand_string_internal (string=0x5555558a6b28 "\001\002\003\n", quoted=2) at subst.c:3649
#3  0x00005555555c2088 in expand_string_leave_quoted (string=0x5555558a6b28 "\001\002\003\n", quoted=2) at subst.c:3777
#4  0x00005555555c2197 in expand_string (string=0x5555558a6b28 "\001\002\003\n", quoted=2) at subst.c:3825
#5  0x00005555555f203b in write_here_document (fd=3, redirectee=0x5555558a6b08) at redir.c:394
#6  0x00005555555f227f in here_document_to_fd (redirectee=0x5555558a6b08, ri=r_reading_until) at redir.c:476
#7  0x00005555555f2f8b in do_redirection_internal (redirect=0x5555558a6b48, flags=1) at redir.c:970
#8  0x00005555555f1cba in do_redirections (list=0x5555558a6b48, flags=1) at redir.c:234
#9  0x00005555555a347d in execute_disk_command (words=0x5555558a5b08, redirects=0x5555558a6b48, command_line=0x5555558a5408 "cat  <<    EOF\n\001\002\003\nEOF\n", pipe_in=-1, pipe_out=-1, async=0,
    fds_to_close=0x5555558a6b88, cmdflags=0) at execute_cmd.c:5212
#10 0x00005555555a1ced in execute_simple_command (simple_command=0x5555558a6ac8, pipe_in=-1, pipe_out=-1, async=0,     fds_to_close=0x5555558a6b88) at execute_cmd.c:4386
#11 0x000055555559b098 in execute_command_internal (command=0x5555558a6a88, asynchronous=0, pipe_in=-1, pipe_out=-1,     fds_to_close=0x5555558a6b88) at execute_cmd.c:802
#12 0x000055555559a6a6 in execute_command (command=0x5555558a6a88) at execute_cmd.c:405
#13 0x00005555555841ff in reader_loop () at eval.c:180
#14 0x0000555555581bc3 in main (argc=2, argv=0x7fffffffdb68, env=0x7fffffffdb80) at shell.c:792

expand_word_internal "\001\002\003" "\001\002\001\003\" "\001\001\001\002\001\003", , , .

+2

Source: https://habr.com/ru/post/1695594/


All Articles