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

I am using PHP exec() to start a process in Bash that has $$ on its command line. When using PHP, however, PHP itself seems to accept the $$ variable instead of allowing Bash to use it in the script.

Does PHP use this variable? Assuming this, how to save it for a bash script?

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

+4
source share
2 answers

Php will not take the value $$ , since it is inside a string with a single quote.

It bash converts it to the PID of the bash process, processing your echo command.

If you want to literally print two $ through the echo command, you will need to avoid 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 
+8
source

Like @marc, exec actually returns the PID of the process.

However, this is still a “dodgy” syntax for use in PHP, as you have to be careful to put it in, '' otherwise PHP will do something special with it. Basically, PHP has the ability to use variables in two ways:

 $a 

and

 $$a 

The latter, using the value of $ a to actually jump to the new variable name, so you have to be careful how you use this syntax (http://www.php.net/manual/en/language.variables. Variable.php) .

+1
source

All Articles