How to remove space from string?
Since you're using bash, the fastest way would be:
shopt -s extglob # Allow extended globbing
var=" lakdjsf lkadsjf "
echo "${var//+([[:space:]])/}"
It's fastest because it uses built-in functions instead of firing up extra processes.
However, if you want to do it in a POSIX-compliant way, use sed
:
var=" lakdjsf lkadsjf "
echo "$var" | sed 's/[[:space:]]//g'
The tools sed
or tr
will do this for you by swapping the whitespace for nothing
sed 's/ //g'
tr -d ' '
Example:
$ echo " 3918912k " | sed 's/ //g'
3918912k
Try doing this in a shell:
var=" 3918912k"
echo ${var//[[:blank:]]/}
That uses parameter expansion (it's a non posix feature)
[[:blank:]]
is a POSIX regex class (remove spaces, tabs...), see http://www.regular-expressions.info/posixbrackets.html