One of the most powerful things about Linux is the terminal (or called ‘shell’ at many places). A lot of automation of Linux platforms can be credited to the scripting ability of Linux shells. One of the common requirements of scripting is to strip off spaces and newlines.

NOTE: You might think that Ubuntu or Debian use bash as their terminal, but they don’t. They instead use dash which behaves mostly the same but uses less memory, follows the POSIX standards most closely and is generally faster.

Why do we need to remove spaces and newlines?

One of the most basic things we do when automating tasks is to use the output from one command as an input to another command. For example, you might want to list files that end with .txt and pass them as an argument to another command which prints the number of lines in each file. A number of time, commands (or tools that can run on the shell) expect the input to be exact. In our example, if you sent a filename containing newlines or spaces, the tool which prints the number of lines might not even find the file (because such a file does not exist)! But it is fairly possible that the output of ls contained spaces or newlines (depending on what arguments you passed to ls).

In cases like these, we have to remove spaces and newlines from the output of a command.

How to remove all kinds of spaces?

When we say spaces, we usually mean whitespaces which appear as a single blank space on the terminal. However, there are other characters such a the newline character (\n) or the carriage return character (\r) or the tab character (\t) which also cause blank spaces on the terminal (or in other editors).

blank-spaces-on-linux-terminal

Note how tabs and newlines cause blank spaces to display

When we want to remove all those characters at one go, we can use xargs. The primary use of xargs is to convert text into a format which can be used as arguments to another program. Removing newlines and tabs is a part of that process. If you pass such a line to xargs, it would simply remove all the extra spaces and newlines. For example, if you did this:

echo -e "hello\nI am on new line\n\tand I am                    indented" | xargs

Then your output would be:

hello I am on new line and I am indented

How to remove newlines

While xargs can be used almost always, you can also use tr to replace strings from a given text.

You can use:

echo -e "This \n is \n multiline" | tr -d '\n'

You will get the text without the newline character!

tr is used to replace sub-strings from a given text. The -d switch is used to drop or delete the given argument from the input

Summary

Since xargs is available on almost all UNIX-like platforms (Linux, macOS, BSD flavors), it is the preferred way to remove all kinds of newline and blank space characters. However, tr can also be used to do the same.