Search code examples
phpshellshell-execpassthru

PHP shell_exec / passthru showing output different from actual shell output


I am trying to display the output of a shell command using PHP's passthru function. I am getting an extra [0;32m where the actual shell output has a green line and am also getting an extra [m at the beginning of few other lines.

What do I need to do to get a PHP output without these additional characters?

My PHP command is:

passthru('/var/www/cCompiledScript');

Solution

  • I don't know what cCompiledScript is, but clearly it is outputing ANSI escape sequences to generate color on the terminal. It may have a command line flag or environment variable that you can set to disable color output. If you wrote cCompiledScript you could add a flag to disable color. Otherwise you will have to strip out the color codes:

    passthru('/var/www/cCompiledScript | sed "s/\x1B\[\([0-9]\{1,2\}\(;[0-9]\{1,2\}\)\?\)\?[mGK]//g"');
    

    The sed command above is from this answer on the Unix & Linux Stack Exchange.