0

I am calling a shellscript from php using shell_exec() command. Following is the simple version of the shell script:

args[0] = "tom"
echo "hello"
echo "${args[0]}"

When I run this script from terminal, it gives the following output in terminal:

hello
tom

Whereas when I call this from php using shell_exec() only "hello" is printed and not "tom" . ie; variable assignment is not working when the script is called from php. Why this happens and how can I resolve this.

Any help is appreciated.

2 Answers 2

1

Probably PHP executes the script with sh, not Bash; thus arrays (which are a Bash feature) are not supported by the shell.

Workarounds: don't use arrays, or explicitly inboke Bash on the script. (If PHP understands shebangs, having a correct shebang line as the first line of the script may well be sufficient.)

Sign up to request clarification or add additional context in comments.

2 Comments

shell_exec("path/to/file.sh") is how we call the script, which means we are executing this as sh, correct?
Again, adding a proper shebang may be enough to fix it.
0

First, args[0] = "tom" should not have space. It should be args[0]="tom" (I'm not sure is this the problem, but worth to try).

Try this at the end of your code, to see which shell is running your script.

echo "`ps -p $$`"

Try both on terminal and PHP scripts to see if it same shell or not.

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.