Create Arbitrarily Long Strings in Bash

Generating a file filled with a repeated character or string is useful for fuzzing, padding, boundary testing, and placeholder data. The yes | tr | dd pipeline is the classic POSIX-compliant approach.

The classic pipeline

yes $CHARACTER | tr -d '\n' | dd of=$OUTFILE bs=$BLOCK_SIZE count=$COUNT
  • yes $CHARACTER — repeats the character indefinitely
  • tr -d '\n' — strips newlines, creating a continuous stream
  • dd of=$OUTFILE bs=$BLOCK_SIZE count=$COUNT — writes $BLOCK_SIZE * $COUNT bytes

The result contains no trailing newline, making it suitable for direct insertion into another file via "$(cat $OUTFILE)".

Constraints

  • $BLOCK_SIZE must be a power of 2 and no more than 4096 on most systems (matching the typical page size).
  • $COUNT determines how many blocks are written; total bytes = $BLOCK_SIZE * $COUNT.

Alternative: printf + loop

For small strings or when dd feels heavy:

printf '%*s' $COUNT '' | tr ' ' "$CHARACTER" > $OUTFILE

This uses printf to generate spaces and tr to convert them.

Alternative: head from /dev/zero

For null bytes or simple padding:

head -c $COUNT /dev/zero | tr '\0' "$CHARACTER" > $OUTFILE

Sources

Related: bash-scripting, debugging-bash-scripts-set-x