I have a shell script with this code:
var=`hg st -R \"$path\"`
if [ -n \"$var\" ]; then
echo $var
fi
But the conditional code always ex
There are a lot of answers, but I still believe my just-written script is worth being mentioned because:
"$*"
joins multiple arguments using one space. if you want to trim & output only the first argument, use "$1"
insteadThe script:
trim() {
local s2 s="$*"
until s2="${s#[[:space:]]}"; [ "$s2" = "$s" ]; do s="$s2"; done
until s2="${s%[[:space:]]}"; [ "$s2" = "$s" ]; do s="$s2"; done
echo "$s"
}
Usage:
mystring=" here is
something "
mystring=$(trim "$mystring")
echo ">$mystring<"
Output:
>here is
something<