Can anyone help me with my current problem. How do you redirect output
from a command into a varable?
Thanks
Ed
var="`script`"
or better
var="$(script)"
and perhaps, if you want stderr too:
var="$(script 2>&1)"
Here is what I am trying to redirect.
$ISQL -U${SYBASEUSER} -P${SYBASEPASSWD} -w99999999 <<-!
select 1
go
!
Any Ideas?
All, what is the difference between the var="`script`" and
var="$(script)"? In what situations might one be preferable over the
other?
Thanks,
Sashi
sure:
var="$($ISQL -U${SYBASEUSER} -P${SYBASEPASSWD} -w99999999 <<-!
select 1
go
!)"
None, beyond syntax.
> In what situations might one be preferable over the
> other?
$() allow easy nesting, while `` become a nightmare should the
inner command get complex, and include itself back-quotes.
Forgot to mention old shells, non POSIX, and exotic ones may
not support the latter, while the former is generally always
available.
Popular shells like bash and ksh do support $().
The variable var *is* containing the command's result.
Did you try running what I sent ?
> [ `` vs $() ]
> Forgot to mention old shells, non POSIX, and exotic ones may
> not support the latter, while the former is generally always
> available.
>
> Popular shells like bash and ksh do support $().
However, their parser is not very robust here.
bash-2 and -3, ksh88 and zsh fail for these:
echo $(
case x in x) echo x;; esac
)
echo $(
echo comment # with )
)
echo $(
cat <<\eof
' # or a single back- or doublequote
eof
)
You have to use "case x in (x)" or escape the problematic character,
respectively. Ash variants and ksh93 on the other hand come with a
robust parser.
Thus, IMHO it's just a matter of taste (and need for portability).
> [ `` vs $() ]
> Forgot to mention old shells, non POSIX, and exotic ones may
> not support the latter, while the former is generally always
> available.
>
> Popular shells like bash and ksh do support $().
However, their parser is not very robust here.
bash-2 and -3, ksh88 and zsh fail for these:
echo $(
case x in x) echo x;; esac
)
echo $(
echo comment # with )
)
echo $(
cat <<\eof
here-doc with )
eof
Indeed, while the first example isn't really a bug, but a lack
of upward compatibility, the last two are clearly bugs that should
be fixed in bash.
> Thus, IMHO it's just a matter of taste (and need for portability).
Beyond the fact that nesting is far clearer with it, I like the
parenthesis syntax because under vi, I can easily jump from the
beginning to the end with the "%" command.
>> However, their parser is not very robust here.
>> bash-2 and -3, ksh88 and zsh fail for these:
>>
>> echo $(
>> case x in x) echo x;; esac
>> )
> [...] isn't really a bug, but a lack of upward compatibility
I am not sure what you mean here?
I mean that the new case syntax with balanced braces become
mandatory here, breaking compatibility with the previous one.
I guess you mean it _should_ become mandatory, in new shell releases?
(Currently, it's optional in both SUSv3 and implementations.)
ksh88 for example accepts "case x in (x)" but even does not document it.
Thus it's a bug - with an existing workaround.
Yes, a documentation bug. The new syntax is de facto mandatory, but
nothing tells it in the manual pages.