Removing all spaces, tabs, newlines, etc from a variable?

The reason sed 's/[[:space:]]//g' leaves a newline in the output is because the data is presented to sed a line at a time. The substitution can therefore not replace newlines in the data (they are simply not part of the data that sed sees).

Instead, you may use tr

tr -d '[:space:]'

which will remove space characters, form feeds, new-lines, carriage returns, horizontal tabs, and vertical tabs.


In ksh, bash or zsh:

set_jobs_count=…
set_jobs_count=${set_jobs_count//[[:space:]]/}

In any Bourne-like shell, you can remove leading and trailing whitespace and normalize all intermediate whitespace to a single space like this:

set -f
set -- $set_jobs_count
set_jobs_count="$*"
set +f

set -f turns off globbing; if you know that the data contains none of the characters \[?*, you can omit it.

That assumes $IFS contains its default value of space, tab, newline. Other whitespace characters (CR, VT, FF...) are left alone.