On 22 Aug 2006 07:16:08 -0700,
mr.bmonroe@gmail.com wrote:
> Hey all,
>
> I have a script (using ksh88) that takes in STDIN and stores it within
> a variable.
>
> Is it more apropriate to use 'cat' to do this ala:
>
> FOO=$(cat)
>
> or is it acceptabe (prefered) to do it this way:
>
> FOO=$(</dev/fd/0)
>
> This is how I am doing it now but am I setting my self up for trouble
> in the future?
[...]
FOO=$(cat)
is the correct way. $(<...) is shell dependant and /dev/fd/0 is
not available on all systems.
Please note that will both approaches, all the trailing newline
characters in stdin will be trimmed.
Use
FOO=$(cat; echo .); FOO=${FOO%.}
to work around it.
Another way to do it, less legible but may be faster for small
files:
FOO=
NL='
'
while IFS= read -r line; do
FOO=$FOO$line$NL
done
FOO=$FOO$line
In every case, remember that the NUL character can not be stores
in a shell variable, so you can't process binary files that way.
--
Stephane