BASH Shell使用变量中的空格扩展参数 [英] BASH shell expand arguments with spaces from variable
问题描述
说我有一个变量$ARGS
,其中包含以下内容:
file1.txt "second file.txt" file3.txt
如何将$ARGS
的内容作为参数传递给命令(例如说cat $ARGS
),将"second file.txt"
视为一个参数,而不将其拆分为"second
和file.txt"
? >
理想情况下,我希望能够将参数完全存储在变量中(从文本文件中读取,但我认为不相关)将参数传递给任何命令.
谢谢!
在不使用bash数组或eval
的情况下可以做到这一点:这是xargs
在不使用-0
的情况下为数不多的行为之一或-d
扩展名(通常会产生错误的行为)实际上很有用.
# this will print each argument on a different line
# ...note that it breaks with arguments containing literal newlines!
xargs printf '%s\n' <<<"$ARGS"
...或...
# this will emit arguments in a NUL-delimited stream
xargs printf '%s\0' <<<"$ARGS"
# in bash 4.4, you can read this into an array like so:
readarray -t -d '' args < <(xargs printf '%s\0' <<<"$ARGS")
yourprog "${args[@]}" # actually run your programs
# in bash 3.x or newer, it's just a bit longer:
args=( );
while IFS= read -r -d '' arg; do
args+=( "$arg" )
done < <(xargs printf '%s\0' <<<"$ARGS")
yourprog "${args[@]}" # actually run your program
# in POSIX sh, you can't safely handle arguments with literal newlines
# ...but, barring that, can do it like this:
set --
while IFS= read -r arg; do
set -- "$@" "$arg"
done < <(printf '%s\n' "$ARGS" | xargs printf '%s\n')
yourprog "$@" # actually run your program
...或者让xargs
自己进行调用:
# this will call yourprog with ARGS given
# ...but -- beware! -- will cause bugs if there are more arguments than will fit on one
# ...command line invocation.
printf '%s\n' "$ARGS" | xargs yourprog
Say I have a variable $ARGS
which contains the following:
file1.txt "second file.txt" file3.txt
How can I pass the contents of $ARGS
as arguments to a command (say cat $ARGS
, for example), treating "second file.txt"
as one argument and not splitting it into "second
and file.txt"
?
Ideally, I'd like to be able to pass arguments to any command exactly as they are stored in a variable (read from a text file, but I don't think that's pertinent).
Thanks!
It's possible to do this without either bash arrays or eval
: This is one of the few places where the behavior of xargs
without either -0
or -d
extensions (a behavior which mostly creates bugs) is actually useful.
# this will print each argument on a different line
# ...note that it breaks with arguments containing literal newlines!
xargs printf '%s\n' <<<"$ARGS"
...or...
# this will emit arguments in a NUL-delimited stream
xargs printf '%s\0' <<<"$ARGS"
# in bash 4.4, you can read this into an array like so:
readarray -t -d '' args < <(xargs printf '%s\0' <<<"$ARGS")
yourprog "${args[@]}" # actually run your programs
# in bash 3.x or newer, it's just a bit longer:
args=( );
while IFS= read -r -d '' arg; do
args+=( "$arg" )
done < <(xargs printf '%s\0' <<<"$ARGS")
yourprog "${args[@]}" # actually run your program
# in POSIX sh, you can't safely handle arguments with literal newlines
# ...but, barring that, can do it like this:
set --
while IFS= read -r arg; do
set -- "$@" "$arg"
done < <(printf '%s\n' "$ARGS" | xargs printf '%s\n')
yourprog "$@" # actually run your program
...or, letting xargs
itself do the invocation:
# this will call yourprog with ARGS given
# ...but -- beware! -- will cause bugs if there are more arguments than will fit on one
# ...command line invocation.
printf '%s\n' "$ARGS" | xargs yourprog
这篇关于BASH Shell使用变量中的空格扩展参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!