How can you limit the count of standard output characters that is redirected to file?
Other ways (external)
echo $out| head -c 20
echo $out | awk '{print substr($0,1,20) }'
echo $out | ruby -e 'print $_[0,19]'
echo $out | sed -r 's/(^.{20})(.*)/\1/'
You could use Command Substitution to wrap the output pre-redirection, then use the offset Parameter Expansion to limit the number of characters like so:
#!/bin/bash
limit=20
out=$(echo "this line has more than twenty characters in it")
echo ${out::limit} > /path/to/file
Proof of Concept
$ limit=20
$ out=$(echo "this line has more than twenty characters in it").
$ echo ${out::limit}
this line has more t
You can't do so directly into a file, but you can pipe through sed
or head
, etc. to pass on only part of the output. Or as @SiegeX says, capture the output in the shell (but I would be wary of that if the output mis likely to be large).
精彩评论