这是测试代码。
\ndeclare -a arr=(12 34 56 78 90)\n\nIFS=\',\'\n\ncat <<<"${arr[*]:0:3}"\n\ncat <<EOF\n${arr[*]:0:3}\n${arr[*]}\nEOF\nRun Code Online (Sandbox Code Playgroud)\n结果:
\n\xe2\x9d\xaf bash test.sh\n12,34,56\n12 34 56\n12,34,56,78,90\n\n\xe2\x9d\xaf zsh test.sh\n12,34,56\n12,34,56\n12,34,56,78,90\nRun Code Online (Sandbox Code Playgroud)\n如您所见,,使用 bash 时缺少第二行的 。
问题是您正在使用herestring和heredoc并且希望IFS以相同的方式使用,同时在数组上使用扩展。
每三个扩展的工作原理略有不同。
\n这里的文档:
\n$ man bash | sed -n '/Here Doc/,/No/p'\nHere Documents\n This type of redirection instructs the shell to read input from the current source until a\n line containing only delimiter (with no trailing blanks) is seen. All of the lines read up\n to that point are then used as the standard input (or file descriptor n if n is specified)\n for a command.\n\n The format of here-documents is:\n\n [n]<<[-]word\n here-document\n delimiter\n\n No parameter and variable expansion, command substitution, arithmetic expansion, or path\xe2\x80\x90\nRun Code Online (Sandbox Code Playgroud)\n这里的字符串:
\n$ man bash | sed -n '/Command Substitution/,/Bash/p'\nCommand Substitution\n Command substitution allows the output of a command to replace the command name. There are\n two forms:\n\n $(command)\n or\n `command`\n\n Bash performs the expansion by executing command in a subshell environment and replacing\nRun Code Online (Sandbox Code Playgroud)\n一个工作示例:
\n#!/bin/bash\n\ndeclare -a arr=(12 34 56 78 90)\n\n# Setting IFS for the entire script\nIFS=','\n\necho "Here String with variable expansion, IFS=',' is working:"\ncat <<<"${arr[*]:0:3}"\n\necho "Here Document without variable expansion, IFS=',' is not working, because it is not inside cat <<EOF...EOF:"\ncat <<EOF\n${arr[*]:0:3}\nEOF\n\necho "Echoing with command substitution in a subshell before using heredoc"\ncat <<EOF\n$(IFS=','; echo "${arr[*]:0:3}")\nEOF\n\necho "Printig with command substitution in a subshell before using heredoc"\ncat <<EOF\n$(IFS=','; printf "%s" "${arr[*]:0:3}")\nEOF\nRun Code Online (Sandbox Code Playgroud)\n输出:
\nHere String with variable expansion, IFS=',' is working:\n12,34,56\nHere Document without variable expansion, IFS=',' is not working, because it is not inside cat <<EOF...EOF:\n12 34 56\nEchoing with command substitution in a subshell before using heredoc\n12,34,56\nPrintig with command substitution in a subshell before using heredoc\n12,34,56\nRun Code Online (Sandbox Code Playgroud)\n解释:
\n需要在定界文档块中使用命令替换(例如 $(...))以利用 IFS 进行数组分隔,这与 Bash 如何支持定界文档块中的文本扩展和命令替换解释有关。
\n默认情况下,在heredoc块中,文本扩展的执行方式与脚本其他部分相同,包括使用IFS。但是,在某些情况下,如果更改分隔符,则 IFS 和 Heredoc 块中的数组扩展之间的交互可能会产生意外结果。
\n使用命令替换(例如$(IFS=','; echo "${arr[*]:0:3}"))允许对替换块内的定界符进行显式控制,并确保仅针对替换中执行的命令更改 IFS。这对于实现所需的分离很有用,而无需更改整个 Heredoc 块或脚本的 IFS 设置。
简而言之,在heredoc块中使用命令替换可以更好地控制IFS和其他环境变量,而不影响heredoc块的整个环境。这是一种确保 IFS 仅针对heredoc 块中需要的文本特定部分进行更改的方法。
\n也许这有帮助。
\n