Hi,
I want to trim the spaces on left side of the variable value. For eg: if it is 4 spaces followed by value, All the spaces on the left should be supressed.
Easy but want it quickly.
Regards,
Shiv@jad
Hi,
I want to trim the spaces on left side of the variable value. For eg: if it is 4 spaces followed by value, All the spaces on the left should be supressed.
Easy but want it quickly.
Regards,
Shiv@jad
$ echo " something here"|sed 's/^[ \t]*//'
OutPut
something here
Is there any direct function like trim?
Not really. You could come up with a clever ${var#something} substitution but it would be rather more complex.
echo "${var#${var%?${var#*[! ]}}}"
There's a space and a tab between [! and ].
If it is safe to not quote the variable, then simply using it without quotes will discard leading and trailing whitespace, and compress any internal whitespace.
echo $a | tr -s "^ "
That only worked because you forgot to properly quote "$a" (at least it doesn't work here; tr does not look at context, and does not understand ^ to mean beginning of line; tr version from GNU coreutils 6.10).
if you have Python, here's an alternative
echo " something here" |python -c "print raw_input().strip()"