Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

php

I am trying to run a PHP function inside Bash... but it is not working.

#! /bin/bash

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

In the reality, I needed to keep the return value in a bash variable... By the way, I am using the php's 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?

like image 781
Roger Avatar asked Jun 03 '11 19:06

Roger


1 Answers

php_cwd=`/usr/bin/php << 'EOF'
<?php echo getcwd(); ?>
EOF`
echo "$php_cwd" # Or do something else with it
like image 165
phihag Avatar answered Oct 04 '22 11:10

phihag