I have this bash(version 3.2 on MacOS) function
rtf(){
local filter=${1//*/(.)*};
local bin="vendor/bin/phpunit --filter="
local command=$bin"'/"$filter"/i'";
echo $command;
$command;
}
What it’s suppose to do is run phpunit with proper regex, here i am converting *
to (.)*
and wrapping filter to be case insensitive.
Now
$ rtf test*fail
// results in
vendor/bin/phpunit --filter='/test(.)*fail/i'
The problem is that parsed command works perfectly fine(matched test cases run), when i run directly at terminal. But it does not when function rtf
tries to call it in last line. Somehow phpunit can not find matchings for this.
I can believe that the issue is with forward slash in the $command
variable. Can any suggest a fix?
Advertisement
Answer
You should use eval "$command"
rather than $command
. See following example:
[STEP 101] $ cmd="echo 'foo'"
[STEP 102] $ $cmd
'foo'
[STEP 103] $ eval "$cmd"
foo
[STEP 104] $
And it’s not safe to write rtf test*fail
, instead it should be rtf "test*fail"
otherwise test*fail
will be expanded to filenames if the pattern matches some files.