cwd
cwd

Reputation: 54776

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?

Upvotes: 4

Views: 3372

Answers (1)

knittl
knittl

Reputation: 265201

Does escapeshellcmd or escapeshellarg not do what you want?

shell_exec('echo '. escapeshellarg($variable));

Upvotes: 11

Related Questions