4

Possible Duplicate:
pass parameters to php with shell

I am running a php script within shell script as

#! /bin/sh

php file.php

Shell will run the script and display the output of the php script, but how can I pass variable defined within php script into shell script to be used therein (for further processing by shell)?

For example, consider a php file of

<?php
$test = "something";
?>

How can I pass the value of $test to the shell script as

#! /bin/sh

php config.php
echo $test

UPDATE: The methods suggested are based on printing the variables. I prefer not to print out anything, as the php script has other applications too (to be included in other php scripts).

1

2 Answers 2

3

Your PHP script can print shell-style variable assignments:

print("VAR1=foo\n");
print("VAR2=bar\n");

In your shell script, you will need to evaluate these assignments, in order to import them into your environment:

. <(php file.php)
Sign up to request clarification or add additional context in comments.

Comments

1

Your PHP script can print the values and your shell script can use process or command substitution or read to retrieve the values.

PHP:

<?php
    print($test1 . "\n");
    print($test2 . "\n");
?>

Bash:

while read -r line
do
    something_with "$line"
done < <(php_script)

or

saveIFS=$IFS
IFS=$'\n'
array=($(php_script))
IFS=$saveIFS

or

saveIFS=$IFS
IFS=$'\n'
read -r -d '' var1 var2 <<< "$(php_script)"
IFS=$saveIFS

or other variations.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.