Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PHP use $$ with exec(), and how can I preserve it for the next script?

Tags:

bash

php

I use PHP exec() to start a process in Bash that has $$ in its commandline. When using PHP though, PHP itself seems to take the variable $$ instead of letting Bash use it in the script.

Does PHP use this variable? Assuming so, how do I preserve it for the Bash script?

Example: exec('echo $$') performs echo 1538 in Bash, not echo $$, since PHP seems to have taken the variable $$.

like image 751
hexacyanide Avatar asked Jan 15 '23 19:01

hexacyanide


1 Answers

Php would not 'take' the $$ value, since it's inside a single-quoted string.

It's bash converting it to the PID of the bash process handling your echo command.

If you want to literally output two $ via the echo command, you'll have to escape them:

exec('echo \\$\\$');

followup:

marc@panic:~$ bash
marc@panic:~$ echo $$
31285
marc@panic:~$ php -a
Interactive shell

php > echo exec('echo $$');
31339
php > echo exec('echo \\$\\$');
$$

followup 2:

marc@panic:~$ cat pid
#!/bin/bash
echo $$
marc@panic:~$ ./pid    <--new shell started to execute script
31651
marc@panic:~$ . pid    <---script executed within context of current shell
31550
marc@panic:~$ echo $$
31550
like image 166
Marc B Avatar answered Jan 30 '23 23:01

Marc B