I'm trying to write a bash script that will let me save a backup code (lots of numbers) in a file. I've finished the script but it's only letting me to save 4096 digits of the code.

I tried to do this:

# Ask for backup code
read -p "Backup code:" backupcode
# Check backup code length
if (( l > m )); then
    echo -e "${RED}ERROR:${NC} Backup is too large! The limit is 4096 digits."
    # Save backup code in the file
    echo $backupcode > "${path[$i]}"

I think this didn't detect that the backup was too large. So, I think there's something with read command.

If there's a limit in read, are there any alternatives I can use?

1 Answer 1


There's no limit for the read command itself. But there's a limit for how much you can type on a single line in the terminal. To see this, try running the command wc -c and typing a very long line. You'll hit that same limit at 4096 bytes.

To input more than the limit, either arrange to make the code multi-line with each line being short enough, or input it in some other way than directly reading from the terminal in cooked mode.

If you enable readline, bash reads characters one by one, and there's no limit to the line length other than available memory.

read -e -p "Backup code:" backupcode

However, reading such a long input from the terminal is a very bad user interface. A user isn't going to sit there and type thousands of characters. Instead, read input from the clipboard or from a file.

  • Thank you. I'm saving the backup from the clipboard so that should be much easier.
    – adazem009
    Jun 30, 2020 at 20:53
  • I have encountered limit of 4095 characters when reading from a file: while read -r line ; do ... printf %s\\n "$line" >>"$failed_file" --- this writes the line truncated to 4095 characters ... done < "$in_file". GNU bash, version 4.4.19(1)-release (x86_64-redhat-linux-gnu) Nov 19, 2021 at 14:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .