Run the PHP function inside Bash (and save the return in a Bash variable)

I am trying to run a PHP function inside Bash ... but it does not work.

#! /bin/bash /usr/bin/php << 'EOF' <?php echo getcwd(); ?> EOF 

In reality, I needed to store the return value in a bash variable ... By the way, I use the phc getcwd () function only to illustrate the bash operation.

UPDATE: Is there a way to pass a variable?

 VAR='/$#' php_cwd=`/usr/bin/php << 'EOF' <?php echo preg_quote($VAR); ?> EOF` echo "$php_cwd" 

Any ideas?

+7
source share
7 answers
 php_cwd=`/usr/bin/php << 'EOF' <?php echo getcwd(); ?> EOF` echo "$php_cwd" # Or do something else with it 
+8
source
 PHP_OUT=`php -r 'echo phpinfo();'` echo $PHP_OUT; 
+8
source

As an alternative:

 php_cwd = `php -r 'echo getcwd();'` 

replace getcwd (); call with your php code if necessary.

EDITOR: Ninja David Chan.

+3
source

So you can embed PHP commands in a shell, i.e. * sh:

 #!/bin/bash export VAR="variable_value" php_out=$(php << 'EOF' <? echo getenv("VAR"); //input ?> EOF) >&2 echo "php_out: $php_out"; #output 
+1
source

This is what worked for me:

 VAR='/$#' php_cwd=`/usr/bin/php << EOF <?php echo preg_quote("$VAR"); ?> EOF` echo "$php_cwd" 
0
source

Use the php command line '-R'. It has a built-in variable that reads the input.

 VAR='/$#' php_cwd=$(echo $VAR | php -R 'echo preg_quote($argn);') echo $php_cwd 
0
source

My question is: why aren't you using functions to print the current working directory in bash? How:

 #!/bin/bash pwd # prints current working directory. 

Or

 varibable=`pwd` echo $variable 

Edited . The code above has been modified to work without problems.

-one
source

All Articles