Skip to content

Instantly share code, notes, and snippets.

@josharian
Created July 28, 2025 00:36
Show Gist options
  • Save josharian/12bb33defb0aeeb11b66742b53243bb5 to your computer and use it in GitHub Desktop.
Save josharian/12bb33defb0aeeb11b66742b53243bb5 to your computer and use it in GitHub Desktop.
# Test commands for null byte behavior across shells
# Lines starting with # are comments and will be ignored
# Empty lines are also ignored
# Basic printf tests - these should work identically
printf "hello"
printf "before\\x00after"
printf "before\\000after"
printf "test\\x00"
printf "\\x00test"
printf "a\\x00b\\x00c"
# Printf with format specifiers
printf "%s\\n" "test"
printf "%s" "before\\x00after"
# Echo tests - different shells may behave differently
echo "before\\x00after"
echo -e "before\\x00after"
# Dollar quote expansion - should stop at null bytes
echo $'before\\x00after'
echo $'test\\x00'
echo $'\\x00after'
# Variable assignments and expansions
a=$'test\\x00after'; echo "$a"
a=$'test\\x00after'; printf "%s" "$a"
a=$'test\\x00after'; echo ${#a}
# Command substitution with null bytes
echo "$(printf "before\\x00after")"
echo "$(echo $'test\\x00after')"
# More complex printf cases
printf "line1\\nline2\\x00line3\\nline4"
printf "col1\\tcol2\\x00col3\\tcol4"
# Printf format specifiers with variables containing nulls
a=$'test\\x00after'; printf "%s\\n" "$a"
a=$'test\\x00after'; printf "<%s>" "$a"
# Edge cases
printf ""
printf "\\x00"
printf "test\\x00"
# Multiple escape sequences
printf "\\a\\b\\x00\\n\\t"
echo $'\\a\\b\\x00\\n\\t'
# Test single vs double backslashes in different contexts
printf "test\\\\x00after"
echo "test\\\\x00after"
echo $'test\\\\x00after'
# Mixed quotes and expansions
echo "before$(printf "\\x00")after"
printf "%s" "$(echo $'test\\x00after')"
# Array-like behavior (if supported)
set -- $'a\\x00b' $'c\\x00d'; echo "$1|$2"
# Variable parameter expansion with nulls
a=$'test\\x00after'; echo "${a%after}"
a=$'test\\x00after'; echo "${a#test}"
# More printf format variations
printf "%c" ""
printf "%c" $'\\x00'
printf "%d\\x00%d" 1 2
# Test null bytes in different positions
printf "\\x00"
printf "a\\x00"
printf "\\x00b"
printf "a\\x00b"
# Complex variable manipulations
a=$'prefix\\x00suffix'; b="$a"; echo "$b"
a=$'prefix\\x00suffix'; printf "%s" "$a" | wc -c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment