Skip to content

Instantly share code, notes, and snippets.

@ole-tange
Last active March 25, 2022 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ole-tange/88ae153797748b3618e2433377e2870a to your computer and use it in GitHub Desktop.
Save ole-tange/88ae153797748b3618e2433377e2870a to your computer and use it in GitHub Desktop.
Show mixing can happen with echo/printf - even if the argument is less than 4 KB
#!/bin/bash
# Show that mixing of output can occur with 'echo' with less than 4 KB argument
echo testing echo - any output indicates that mixing has taken place.
# Generate 20000 lines with: echo aa...a &
# echo bb...b &
# where aa...a = 4000 a's, and bb...b = 4000 b's.
perl -e '$a="a"x4000; $b="b"x4000; for(1..10000) { print "echo $a &\n echo $b &\n" }' |
# Execute them. Because of '&' the commands will run in parallel
bash |
# Remove duplicate letters: Each line should now be one single letter (a or b)
# If you get lines with multiple letters (e.g. ab, aba, ba, bab, baba)
# then you have output mixing from different commands
tr -s ab |
# Ignore all lines that contain only 'a' or 'b':
grep -Ev '^.$'
# Any output from this will indicate that mixing has taken place.
echo testing printf - any output indicates that mixing has taken place.
# Generate 20000 lines with: printf "aa...a\n" &
# printf "bb...b\n" &
# where aa...a = 4000 a's, and bb...b = 4000 b's.
perl -e '$a=("a"x4000)."\\n"; $b=("b"x4000)."\\n"; for(1..10000) { print "printf \"$a\" & \n printf \"$b\" & \n" }' |
# Execute them. Because of '&' the commands will run in parallel
bash |
# Remove duplicate letters: Each line should now be one single letter (a or b)
# If you get lines with multiple letters (e.g. ab, aba, ba, bab, baba)
# then you have output mixing from different commands
tr -s ab |
# Ignore all lines that contain only 'a' or 'b':
grep -Ev '^.$'
# Any output from this will indicate that mixing has taken place.
# So what _is_ the safe limit?
# Testing the above with 1007/1008 instead of 4000 suggests that 1007 is the limit for printf, and 1008 for echo
# The limit for /bin/echo and /usr/bin/printf is 4095/4096
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment