4 ms·
Simply don't use echo if you want predictable output. Use printf. https://linux.die.net/man/1/printf https://linux.die.net/man/1/printf
by meindnoch 3y ago
Simply don't use echo if you want predictable output. Use printf. https://linux.die.net/man/1/printf https://linux.die.net/man/1/printf
- ndsipa_pomu 3y agoThis is the way (however, the parent's use of "echo" would be fine as it's not using a variable and so won't be interpreting a dash as an extra option etc)
- gdavisson 3y agoecho -n is not safe, because some versions of echo will just print "-n" as part of their output (and add a newline at the end, as usual). In fact, XSI-compliant implementations are required to do this (and the same for anything else you try to pass as an option to echo). According to the POSIX standard[1], "If the first operand is -n, or if any of the operands contain a <backslash> character, the results are implementation-defined." [1] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html#tag_20_37_05 https://pubs.opengroup.org/onlinepubs/9699919799/utilities/e...
- ndsipa_pomu 3y agoThanks - I wasn't aware that echo was that problematic as I target bash (usually v4 and above) from my scripts. I just tested it out with: sh /bin/echo -n "test" /bin/echo: 3: Syntax error: "(" unexpected I didn't realise until recently that printf can also replace a lot of uses of the "date" command which is helpful with logging as it avoids calling an external command for every line logged.
- hddqsb 3y ago> printf can also replace a lot of uses of the "date" command Very cool (but bash-specific). Manual: https://www.gnu.org/software/bash/manual/bash.html#index-printf https://www.gnu.org/software/bash/manual/bash.html#index-pri... > sh /bin/echo -n "test" This is gibberish -- it's trying to execute /bin/echo as if it was a shell script. Maybe you meant: sh -c '/bin/echo -n "test"'
- quesera 3y agoNot all echos accept -n to suppress newlines. printf is always the better choice.