1

Trying to read a long input into a variable from ZSH on MacOS.

 echo "URL: " read URL 

input is always truncated to 1024 chars... if I try and type additional chars nothing happens.

  • Input is copy/pasted from PostMan, its an S3 signed upload URL

  • If I try and delete a few chars from the end (after pasting) I am only able to manually type as many chars as I deleted

  • I tried using the -n option to no avail (nothing gets read into the variable)

How can I read a long input? ~1500 chars

4
  • hm, interesting, it works for me (Linux, middle-mouse copy and paste into read, as well as writing into an fd and reading that: < <(for i in {1..1500}; do printf "%d" $(( i % 10 )); done; printf "\n") read URL, then printf '%s' $URL | wc yields 1500). Commented Mar 14, 2024 at 14:06
  • 1
    Please edit your question to describe the way you're giving characters to the read URL command. Are you truly typing 1500 characters on your keyboard, or are you using a mouse to copy from one window (where the displayed text is wrapped) and paste into the window where your read command is running? Are there carriage return or Control-D characters in the URL you're typing? Commented Mar 14, 2024 at 15:50
  • updated with additional info per the comments Commented Mar 14, 2024 at 18:15
  • Related: Terminal does not accept pasted or typed lines of more than 1024 characters, and similarly on Linux Is there any limit on line length when pasting to a terminal in Linux? (4096 B) Commented Mar 14, 2024 at 20:10

1 Answer 1

1

read itself just reads bytes from the terminal, it has no control over how the terminal reads those bytes. And the terminal has a limited line length which, as far as I recall, can't be changed easily (or at all?). The terminal's line editor is also very crude, just supporting backspace and not other editing commands.

Use vared instead. This is somewhat similar to read (but with different options), but it's specialized to reading from a terminal and it uses zsh's line editor. That makes it a lot more user-friendly, in addition to not having a line length limit.

URL= vared -p "URL: " URL 

vared always reads from the terminal, not from standard input. If you want to support reading input redirected from a file or pipe, check whether standard input is a terminal with the -t condition..

URL= if [[ -t 0 ]]; then vared -p "URL: " URL else read -r URL fi 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.