我需要使用非标准分隔符打印参数的函数(而不是由创建的空格my_func() { echo "$@"; }
).像这样的东西:
$ my_func foo bar baz
foo;bar;baz
Run Code Online (Sandbox Code Playgroud)
参数数量各不相同,我不需要尾随分隔符.有任何想法吗?
my_func() {
local IFS=';' # change the separator used by "$*", scoped to this function
printf '%s\n' "$*" # avoid reliability issues innate to echo
}
Run Code Online (Sandbox Code Playgroud)
...要么...
my_func() {
local dest # declare dest local
printf -v dest '%s;' "$@" # populate it with arguments trailed by semicolons
printf '%s\n' "${dest%;}" # print the string with the last semicolon removed
}
Run Code Online (Sandbox Code Playgroud)
关于"固有的可靠性问题echo
" - 请参阅POSIX规范echo
的"应用程序使用"部分,并注意bash与该标准的一致性因编译时和运行时配置而异.