The easiest way to trim a variable in shell script.
VARIABLE=$( echo $VARIABLE )
The shell automatically removes seemingly excessive whitespaces, which is why you have to double quote variables if you want to preserve them.
External Source:
http://www.unix.com/shell-programming-scripting/138857-how-trim-variable-linux.html#post302431753
Take a look at following example.
#!/bin/bash
VARIABLEA="Ajay "
VARIABLEB=" M Pawar"
echo "$VARIABLEA"
echo "$VARIABLEB"
VARIABLEC=$(echo "$VARIABLEA $VARIABLEB")
echo "$VARIABLEC"
VARIABLEM=$(echo $VARIABLEA)
VARIABLEN=$(echo $VARIABLEB)
echo "$VARIABLEM"
echo $VARIABLEN
VARIABLEO=$(echo "$VARIABLEM $VARIABLEN")
echo $VARIABLEO
Check the output yourself.
No comments:
Post a Comment