bash:“映射”函数参数?

Bla*_*lia 4 bash xargs

在将列表转发到其他命令之前,通过某种转换(例如连接每个字符串)本质上“映射”bash 参数列表的最优雅的方法是什么?我想到了使用xargs,但我似乎无法概念化如何做到这一点。

function do_something {
    # hypothetically
    for arg in "$@"; do
        arg="$arg.txt"
    done

    command "$@"
}

do_something file1 file2 file3
Run Code Online (Sandbox Code Playgroud)

这样的结果将是调用command file1.txt file2.txt file3.txt.

ffl*_*ing 5

您所做的大部分是正确的,只是您需要使用数组来存储新参数:

function do_something {
    array=()
    for arg in "$@"; do
        array+=("$arg.txt")
    done

    command "${array[@]}"
}

do_something file1 file2 file3
Run Code Online (Sandbox Code Playgroud)