[英]Bash 'here document' word/delimiter unquoted
我有以下腳本:
#!/bin/bash
cat << EOF
^A^B^C
EOF
哪里:
所以,文件看起來像這樣:
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.
當我運行腳本時,十進制值1似乎消失了:
./main.sh | xxd
00000000: 0203 0a ...
我必須引用十進制值1的單詞/分隔符( cat << "EOF"
)也出現,但我不明白為什么。
從bash參考手冊中,這是在word
不加引號時發生的情況:
如果word不加引號,則here-document的所有行都要進行參數擴展,命令替換和算術擴展,字符序列\\ newline將被忽略,'\\'必須用於引用字符'\\','$ '和'''。
我的問題是:
當沒有引用單詞/分隔符時,為什么十進制值1會消失? 參數擴展,命令替換和算術擴展如何影響這一點?
正如評論中指出的那樣,我認為這也是一個錯誤。
在我的例子中,Bash最終將here文檔文本轉換為文件,同時這樣做,它將here文檔文本轉換為某種內部字符串(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
”,我相信這是期望的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.