Syntax error near unexpected token `(‘ when running shell script from XAMPP

I’m trying to send a shell command directly from an index.php using the exec function as follows:

exec('shopt -s extglob && cd /opt/lampp/htdocs/testes/xml/ && rm !(*.xml)');

However xampp returns an error in the log:

sh: -c: line 1: syntax error near unexpected token `('
sh: -c: line 1: `rm !(*.xml)'

I already gave permission for xampp to send commands via exec to the machine. Other commands like just removing files without the !() criteria works fine.

When I write rm !(*.xml) directly through the linux terminal works normally.

  • Not sure if it’s a duplicate, but check stackoverflow.com/questions/72599583/… to see if that helps explain it

    – 

  • Right, i understood. Then I need to use another command without ()? Which command is equivalent to rm !(*.xml)?

    – 




  • 1

    What’s the point in doing a cd in one shell, and the rm in a completely unrelated shell?

    – 

  • The rm works in the terminal, because you have turned extglob on in the same shell where you run the command. In your PHP program, you turn it on in a different shell.

    – 

  • @user1934428 i already tried do it in a same shell in begin, but get the same problem, the code above it’s like this cause it was my last try.

    – 

Leave a Comment