Your terminal settings are different if you press Ctrl + t , unlike when you run a script through the terminal. If you add the following line to read.sh , it will print your terminal settings:
echo Terminal settings: "$(stty -a)"
Now run the script yourself, and then run it by pressing Ctrl + t . You will notice several differences, the biggest of which are the additions -echo and -icrnl , which disable echo and change newline processing. This results in a hanging script.
You can fix this problem inside the script by making tty return to canonical mode and re-add the echo. Before making any changes to stty, you will want to save the settings and restore them when the script exits. You can use trap for this.
#!/bin/bash # Save the tty settings and restore them on exit. SAVED_TERM_SETTINGS="$(stty -g)" trap "stty \"${SAVED_TERM_SETTINGS}\"" EXIT # Force the tty (back) into canonical line-reading mode. stty cooked echo # Read lines and do stuff. echo -n ' > ' read -r buf echo "you typed $buf"
indiv source share