You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bash中移除通配符展开临时数组时的语法错误问题

Ah, I see the issue here—your attempt to wrap the glob in array syntax isn't valid bash. Let's fix that while ditching the temporary array entirely.

First, why your code failed: ${(/tmp/*)[*]} isn't a valid substitution. The glob /tmp/* expands to separate positional arguments, not an array literal you can subscript like that.

The good news is you don't need an array at all here—your existing join function can take the glob expansion directly as arguments. Here's how:

join(){ 
    IFS="$1" 
    shift 
    echo "$*" 
}

# Pass the glob directly to join—no array needed!
SEPARATED_FILES=$(join , /tmp/*)
echo "$SEPARATED_FILES"

When you call join , /tmp/*, the glob expands to all matching files as individual arguments to the function. The join function sets IFS to your separator, shifts it out of the argument list, then uses "$*" to join all remaining arguments with that separator. Perfect!

If you want to handle the case where there are no files in /tmp (so the glob doesn't match anything), enable the nullglob option first—this makes unmatched globs expand to nothing instead of staying as literal text:

shopt -s nullglob  # Turn on nullglob
SEPARATED_FILES=$(join , /tmp/*)
shopt -u nullglob  # Optional: turn it back off if you don't want it affecting other code
echo "$SEPARATED_FILES"

Another option if you don't want to use the join function is to use printf and sed to add commas and trim the trailing one:

shopt -s nullglob
SEPARATED_FILES=$(printf "%s," /tmp/* | sed 's/,$//')
echo "$SEPARATED_FILES"

Either way, you can get your comma-separated list without that temporary FILES array.

内容的提问来源于stack exchange,提问作者Some Name

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:22:09