2

I'm calling a PHP script from command line:

/usr/bin/php -r "require '/path/to/php/dummy.php'; echo getFunction(\"DummyParameter\")";

The call works like a charm, and the PHP function returns the correct value. I want to replace DummyParameter with a $ShellVariable. When I replace DummyParameter, it is supposed to be passed as "$ShellVariable" string. Here is what I have tried:

/usr/bin/php -r "require '/path/to/php/dummy.php'; echo getFunction(\"$ShellVariable\")";

Is there any way to pass the shell variable into the PHP script as a function argument?

Thanks in advance.

1
  • You probably can—but getting proper escaping will possibly be very difficult to get right and will require rather messy code. Is it a proof of concept? Because it's trivial if you just write the code in a file. Commented Nov 29, 2016 at 8:56

1 Answer 1

2

Pass the CLI arguments and use the $argv array, e.g.:

php -r 'printf("%s\n", $argv[1]);' "$(date)"

Sample Output

Tue Nov 29 14:09:07 +07 2016
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.