1

I believe the title speaks for itself but I will elaborate a little more. I have a php file called hello-world.php. I also have a bash script called testBash.sh.

Inside of hello-world.php I have two methods helloWorld() and helloName($name)

Basically what I want to do is from within my bash script (testBash.sh) ... I want to pass in the parameter and execute the helloName($name) method. The parameter will be given from within the bash script.

Here is what I have so far.

testBash.sh

INPUT="Bobby"

// THIS IS WHERE I AM HAVING TROUBLE
TEST= php -r "require 'hello-world.php'; helloName("$INPUT");"

echo "$TEST"

hello-world.php

function helloWorld() {

    return "Hello, World!";

}

function helloName($name) {

    return "Hello, $name!";

}

In a perfect world when I echo "$TEST" the results of that function should be displayed.

Ex: Hello, Bobby!

Is this possible? I have looked online for solutions but this is the closest I have come to find one. Any input or advice would be great. Thanks!

3
  • How about TEST=$(php -r "require 'hello-world.php'; echo helloName(\"$INPUT\");")? Not on a linux computer atm so can't test. Commented Oct 28, 2019 at 23:11
  • Wow, this was exactly what I was looking for. Works perfectly! Would it be possible to elaborate just a bit on this? Commented Oct 28, 2019 at 23:16
  • Sure, I'll add this as an answer with a little explanation then, give me a minute. Commented Oct 28, 2019 at 23:18

1 Answer 1

1

This should do it:

TEST=$(php -r "require 'hello-world.php'; echo helloName(\"$INPUT\");")

I guess this should work as well (single quotes for function param):

TEST=$(php -r "require 'hello-world.php'; echo helloName('$INPUT');")

Basically,

  • adding echo in PHP ensures the php command sends the result of helloName to the output,
  • $(command) tells the bash command to return its output as a value,
  • that value is finally assigned to TEST.
Sign up to request clarification or add additional context in comments.

Comments

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.