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
This does not have the problem with unwanted globbing, also, interior white-space is unmodified (assuming that $IFS
is set to the default, which is ' \t\n'
).
It reads up to the first newline (and doesn't include it) or the end of string, whichever comes first, and strips away any mix of leading and trailing space and \t
characters. If you want to preserve multiple lines (and also strip leading and trailing newlines), use read -r -d '' var << eof
instead; note, however, that if your input happens to contain \neof
, it will be cut off just before. (Other forms of white space, namely \r
, \f
, and \v
, are not stripped, even if you add them to $IFS.)
read -r var << eof
$var
eof