Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a script that prompts the user for input and does some processing afterwards. I was using read to get the data, but then I noticed that when the script is ran with a keyboard shortcut (I added an entry in Ubuntu's Keyboard Shortcut list) it ignores the read and continues with the rest of the code.

How can I read user input with a script running in the background like this?

share|improve this question
up vote 8 down vote accepted

If I understand correctly, you're running the script directly from a GUI environment, not from inside a terminal.

The terminal is what provides the script a way to receive input. If you run the script from the GUI via a menu entry or a keyboard shortcut, the script's input is connected to nothing (this nothing is called /dev/null), so when you ask to read a line, the read command says that there is nothing to read (end of file).

You have two solutions: change the way you call your script, or change your script. The former is simpler and allows your script to be executed inside a terminal sometimes. The latter is more complex but more flexible.

You can easily change your script to run in a terminal. Instead of setting a keyboard shortcut for /path/to/script, set a keyboard shortcut for xterm -e /path/to/script or gnome-terminal -e /path/to/script or whatever terminal emulator you prefer. The terminal window will appear as soon as you press the key and will disappear when your script terminates.

Alternatively, you can change your script to read input through a GUI window instead of from its standard input. Zenity is a common choice for this (there's an Ubuntu package Install zenity). Inside your script, you can use [ -t 0 ] to test whether it's running in a terminal.

if [ -t 0 ]; then
  echo "What is your favorite color?"
  read -r color
  color=$(zenity --entry --text "What is your favorite color?")
share|improve this answer
Thank you. It is exactly what I wanted to know. – nmat Nov 22 '11 at 3:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.