Horen
Horen

Reputation: 11382

php shell_exec adds unwanted line break

Why does php's shell_exec command add an unwanted line break (\r\n) to the output and how can I prevent that?

test.php:

<?php
var_dump(shell_exec('echo "test"'));

running php test.php results in:

string(5) "test
"

Upvotes: 3

Views: 4065

Answers (2)

user703016
user703016

Reputation: 37975

You can pass -n as argument to your echo command, this will prevent echo from outputting a trailing newline.

From the manual:

-n do not output the trailing newline

Upvotes: 2

rekire
rekire

Reputation: 47965

The echo command adds the line break so your example works as expected. If you want to remove it just use trim:

var_dump(trim(shell_exec('echo "test"')));

This will output:

string(5) "test"

Upvotes: 3

Related Questions