2

I need help in passing the Linux bash-script variable to execute the PHP CLI command.

I am working on the bash-script that executes PHP CLI command that gets input variable from the bash-script like folder-path to include a file for accessing a class of the PHP file.

But what happens while executing the bash-script is that the variable passed to the PHP CLI-command act as a variable for PHP.

Below is the sample-code my script

file="$folderpath"somefile.php
mage_version=$(php -r 'include "$file";print_r(Mage::getVersionInfo());')

Below is the error I am getting

PHP Notice:  Undefined variable: file in Command line code on line 1
PHP Warning:  include(): Filename cannot be empty in Command line code on line 1
VinothRaja
  • 1,405
  • 10
  • 21

1 Answers1

3

Bash needs double (") quotes to parse variable, otherwise they will stay $var and so php will read them;

file="$folderpath"somefile.php
mage_version=$(php -r "include \"${file}\";print_r(Mage::getVersionInfo());")

More inline bash info

If you need a multi line solution, you can do something like: Run PHP function inside Bash (and keep the return in a bash variable)

php_cwd=`/usr/bin/php << 'EOF'
<?php echo getcwd(); ?>
EOF`
echo "$php_cwd" # Or do something else with it
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • `php -r 'require '\"${loc}\"'; echo implode(".",$OC_Version);'` I had to use single quotes so that `$OC_Version` was not pre evaluated but taken literally as that is a variable for php not bash – DKebler May 17 '23 at 20:53