Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call php function from bash - with arguments

I have a simple func.php file with concat function:

<?php
function concat($arg1, $arg2)
    {
        return $arg1.$arg2;
    }
?>

I would like to call this function from linux bash shell with two arguments :

1st: "Hello, "
2nd: "World!"

and print the output ("Hello, World!") to linux bash shell.

Please tell me, how to do it?

like image 497
Jake Badlands Avatar asked Mar 03 '26 18:03

Jake Badlands


1 Answers

What you want is $argv

So for example, your script would be called like this:

php /path/to/script.php 'Hello, ' 'World!'

Then in your PHP file:

<?php
$arg1 = $argv[1];
$arg2 = $argv[2];

echo concat($arg1, $arg2);

function concat($arg1, $arg2) {
    return $arg1 . $arg2;
}
like image 96
rjdown Avatar answered Mar 05 '26 08:03

rjdown