How can you limit the count of standard output characters that is redirected to file?
3 Answers
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
2 Comments
sarnold
Cripes, I never cease to be amazed at how much I don't know about bash.
SiegeX
@sarnold Parameter Expansion seems to be the biggest undervalued/unknown feature of not just
bash but any POSIX compliant shell. Time after time I see people calling basename or dirname which both can be done in native shell syntax using Parameter Expansion. See HERE for a good link on usage as well as a great site on bash.