Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape strings in for use in bash/sh via PHP's shell_exec?

I've been calling more advanced shell commands from PHP recently using shell_exec

As my commands become more complicated, I keep experiencing errors with things not being escaped properly. I want to be able to call shell_exec('echo '.$variable) and no matter what I put in $variable it will just echo it. Some things $variable could include are $ ~ ' " \n \r \c `` ( ) { } ; \

What's the best way to escape a shell command before executing it?

like image 218
cwd Avatar asked Oct 15 '25 08:10

cwd


1 Answers

Does escapeshellcmd or escapeshellarg not do what you want?

shell_exec('echo '. escapeshellarg($variable));
like image 102
knittl Avatar answered Oct 16 '25 22:10

knittl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!