Remove spaces or any character from a string (Bash)

From ben.goodacre.name/tech

Jump to: navigation, search

Remove spaces or any character from a string (Bash)

Easy / Poor method

Use standard Bash variable / parameter expansion

test="123 456"
echo ${test/ /}
123456
test="123  456"
echo ${test/ /}
123 456

tr method

Using tr will remove all spaces but is slightly untidy as it involve invoking another command

echo "123    456" | tr -d ' '
123456

Best method: advanced parameter expansion

test="123   456"
echo ${test//[[:space:]]/}
123456

This will also remove trailing and leading spaces.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox