Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to remove all spaces on a file without messing with line endings. This is to be run from inside a bash script.

I have tried all the solutions here: http://stackoverflow.com/questions/9953448/how-to-remove-all-white-spaces-from-a-given-text-file

They work from the command line but when I try them from the bash script the final file is a mess, completely destroyed.

File is using UNIX file endings.

Any clues?

share|improve this question
up vote 4 down vote accepted

You should be able to use tr, but not as specified on the page your link points to as that includes the removal of newline and carriage return. What you should do is:

tr -d " \t" < infile.txt > outfile.txt
share|improve this answer
I have tried that before without success. For some reason, worked now... thanks. – SpaceDog Jul 7 '14 at 18:52

You can pass the [[:blank:]] character class to tr to delete spaces and tabs but retain newlines

<file tr -d '[[:blank:]]'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.