Bash: Strip trailing linebreak from output

BashNewlineLine Breaks

Bash Problem Overview


When I execute commands in Bash (or to be specific, wc -l < log.txt), the output contains a linebreak after it. How do I get rid of it?

Bash Solutions


Solution 1 - Bash

If your expected output is a single line, you can simply remove all newline characters from the output. It would not be uncommon to pipe to the tr utility, or to Perl if preferred:

wc -l < log.txt | tr -d '\n'

wc -l < log.txt | perl -pe 'chomp'

You can also use command substitution to remove the trailing newline:

echo -n "$(wc -l < log.txt)"

printf "%s" "$(wc -l < log.txt)"

If your expected output may contain multiple lines, you have another decision to make:

If you want to remove MULTIPLE newline characters from the end of the file, again use cmd substitution:

printf "%s" "$(< log.txt)"

If you want to strictly remove THE LAST newline character from a file, use Perl:

perl -pe 'chomp if eof' log.txt

Note that if you are certain you have a trailing newline character you want to remove, you can use head from GNU coreutils to select everything except the last byte. This should be quite quick:

head -c -1 log.txt

Also, for completeness, you can quickly check where your newline (or other special) characters are in your file using cat and the 'show-all' flag -A. The dollar sign character will indicate the end of each line:

cat -A log.txt

Solution 2 - Bash

One way:

wc -l < log.txt | xargs echo -n

Solution 3 - Bash

If you want to remove only the last newline, pipe through:

sed -z '$ s/\n$//'

sed won't add a \0 to then end of the stream if the delimiter is set to NUL via -z, whereas to create a POSIX text file (defined to end in a \n), it will always output a final \n without -z.

Eg:

$ { echo foo; echo bar; } | sed -z '$ s/\n$//'; echo tender
foo
bartender

And to prove no NUL added:

$ { echo foo; echo bar; } | sed -z '$ s/\n$//' | xxd
00000000: 666f 6f0a 6261 72                        foo.bar

To remove multiple trailing newlines, pipe through:

sed -Ez '$ s/\n+$//'

Solution 4 - Bash

There is also direct support for white space removal in Bash variable substitution:

testvar=$(wc -l < log.txt)
trailing_space_removed=${testvar%%[[:space:]]}
leading_space_removed=${testvar##[[:space:]]}

Solution 5 - Bash

If you assign its output to a variable, bash automatically strips whitespace:

linecount=`wc -l < log.txt`

Solution 6 - Bash

If you want to print output of anything in Bash without end of line, you echo it with the -n switch.

If you have it in a variable already, then echo it with the trailing newline cropped:

$ testvar=$(wc -l < log.txt)
$ echo -n $testvar

Or you can do it in one line, instead:

$ echo -n $(wc -l < log.txt)

Solution 7 - Bash

printf already crops the trailing newline for you:

$ printf '%s' $(wc -l < log.txt)

Detail:

  • printf will print your content in place of the %s string place holder.
  • If you do not tell it to print a newline (%s\n), it won't.

Solution 8 - Bash

Adding this for my reference more than anything else ^_^

You can also strip a new line from the output using the bash expansion magic

VAR=$'helloworld\n'

CLEANED="${VAR%$'\n'}"

echo "${CLEANED}"

Solution 9 - Bash

Using Awk:

awk -v ORS="" '1' log.txt 

Explanation:

  1. -v assignment for ORS
  2. ORS - output record separator set to blank. This will replace new line (Input record separator) with ""

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhexacyanideView Question on Stackoverflow
Solution 1 - BashSteveView Answer on Stackoverflow
Solution 2 - BashSatyaView Answer on Stackoverflow
Solution 3 - BashTom HaleView Answer on Stackoverflow
Solution 4 - BashAndrey TaranovView Answer on Stackoverflow
Solution 5 - BashnneonneoView Answer on Stackoverflow
Solution 6 - BashRastislav HasicekView Answer on Stackoverflow
Solution 7 - Bashzero2cxView Answer on Stackoverflow
Solution 8 - BashLouisView Answer on Stackoverflow
Solution 9 - BashRakesh GuptaView Answer on Stackoverflow