1

I have a git hook which calls a php file. The php file will produce an output (after running some Unit Tests). Te hook file is a sh file. The output from php file is echoed to the terminal, but \n is stripped, and everything is on a single line. Any ideas what I have to do to have new lines?

Thanks

heXer
  • 304
  • 1
  • 10
  • if you are using print or echo statements in PHP, you need to actually put a \n in the statement itself. Remember that \n won't be interpreted as a newline if you put it inside single quotes. You must use double quotes to allow interpolation, e.g., `print("Hello, World!\n");` – Wolf Jul 23 '15 at 11:17
  • Already did that. echo "Test\n" and echo "Test \r\n" just to be sure. Still does not work. – heXer Jul 23 '15 at 11:25

2 Answers2

2

The solution is to use printf '%s\n' "$output"

The key elements are %s which interprets the output as string, and the double quotes "" which interpret the entire string as a single input. If you don't add the double quotes then every space is replaced by \n, so you would end up with a single word per line. Obviously, the actual string to display is stored in $output.

Reference: http://wiki.bash-hackers.org/commands/builtin/printf

heXer
  • 304
  • 1
  • 10
1

You can try and, in your sh script,

  • assign the output of the php script to a variable avar;
  • echo that variable with:

    echo -e "${avar}"
    

That should keep the newlines, as mentioned in "echo multiple lines into a file".

The same link mentions printf as well.

printf '%s\n' "${avar}"
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks, but echo -e is not supported for me. Nothing is printed. I ended up using printf instead. – heXer Jul 24 '15 at 13:47
  • 1
    @heXer Sure. The link I mentioned has printf as well. I have edited my answer accordingly. – VonC Jul 24 '15 at 13:49