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=$COUNTyes $CHARACTER— repeats the character indefinitelytr -d '\n'— strips newlines, creating a continuous streamdd of=$OUTFILE bs=$BLOCK_SIZE count=$COUNT— writes$BLOCK_SIZE * $COUNTbytes
The result contains no trailing newline, making it suitable for direct insertion into another file via "$(cat $OUTFILE)".
Constraints
$BLOCK_SIZEmust be a power of 2 and no more than 4096 on most systems (matching the typical page size).$COUNTdetermines 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" > $OUTFILEThis 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" > $OUTFILESources
- Stack Overflow — Build a file with large string (dd-based approaches)
- yes(1) — Linux manual page (man7.org)
- dd(1) — Linux manual page (man7.org)
Related: bash-scripting, debugging-bash-scripts-set-x