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
1
  • Try using curly brackets ${folderpath} and ${file} Commented Sep 17, 2019 at 9:37

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.