如何在 Bash 中运行由多个变量分隔的命令?

dar*_*ale 4 command-line bash

我想运行一个由几个“片段”组成的命令。

php="php"
options="-l"

for f in `git diff-index --cached --name-only HEAD | grep -e '\(php\|phtml\)$'`; do
   if [ -f $f ]; then
       # Here I want to run my command defined
       # in $php and $options and put the result in
       # $php_lint var
       # like this command would do:
       # php_lint=$(php -l $f)
       # but with something like that:
       # php_lint=eval $php $options $f
   fi
done
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

更新:

在某个特定点上挣扎之后,我问了这个问题:

请看一下,它也可以帮助您。

ste*_*ver 6

将选项放在一个数组中,并将其展开为 "${options[@]}"

前任。

$ cmd=ls
$ options=(-a -l -d)
$ f="foo"
Run Code Online (Sandbox Code Playgroud)

然后

$ result=$("$cmd" "${options[@]}" "$f")
Run Code Online (Sandbox Code Playgroud)

给予

$ echo "$result"
drwxrwxr-x 2 user user 4096 Apr 20 08:07 foo
Run Code Online (Sandbox Code Playgroud)