防止子字符串中的Bash单词拆分

mar*_*rko 7 linux bash shell

如何防止Bash在子字符串中拆分单词?这是一个有点人为的例子来说明问题:

touch file1 'foo bar'
FILES="file1 'foo bar'"
ls -la $FILES
Run Code Online (Sandbox Code Playgroud)

是否有可能通过$ FILES中的ls命令将'foo bar'视为单个字符串,这将有效地导致与以下命令相同的行为?

ls -la file1 'foo bar'
Run Code Online (Sandbox Code Playgroud)

koj*_*iro 15

使用数组:

files=( file1 'foo bar' )
ls -la "${files[@]}"
Run Code Online (Sandbox Code Playgroud)

  • @marko:不可能 - 你必须修复`s2.sh`.另外,确保脚本实际上是`bash`脚本而不是`/ bin/sh`脚本 - POSIX shell不支持数组. (2认同)