Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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 have this bash script code and i want to run the read command side by side with the while loop as the read command fetch data from user and save it in some file and the while loop do checking on a related something , so any suggestions like doing them in both terminals or something like that


    trap "echo \"\" > /home/........./file.txt"  SIGINT SIGTERM SIGHUP
    while sleep 2
read -s -n 1 key   
    if [[ $key = "" ]]; then 
        echo >> somefile
        echo "You pressed '$key'"
    while sleep $sleepInterval
    while read line
share|improve this question
up vote 2 down vote accepted

This is how I do that:



function READ {

read -t 1 -n 1 bb && ACTION

echo -ne "Hello $a times. Hit \"q\" to quit."'\r' 
a=`echo "$a + 1" | bc`



function ACTION {

if [[ "$bb" == "Q" || "$bb" == "q" ]]; then
    echo "Good bye!"
    exit 0
    echo -e "\n\nYou pressed $bb.\n\n"



And this is what was answered at Stackoverflow by Andy and works great here.

The script is his (I just added the trap part as he says that terminal would be in a weird state if the script gets killed):


trap 'echo -e "\n\n############\n#          #\n# Bye bye  #\n#          #\n############\n" && exit 1' INT 
trap 'notify-send "Bye bye" "The terminal has been closed" && exit 1' HUP

if [ ! -t 0 ]; then
  echo "This script must be run from a terminal"
  exit 1

stty -echo -icanon time 0 min 0

while true; do
  let count+=1
  echo -ne $count'\r'

  # This stuff goes in _handle_keys
  read keypress
  case $keypress in
  # This case is for no keypress
    echo "derecha"
    echo "izquierda"
  # If you want to do something for unknown keys, otherwise leave this out
    echo "unknown input $keypress"
  # End _handle_keys

stty sane
share|improve this answer

i would make the inf. loop its own separate script then when i run it put a ampersand after it

cat /var/log/dmesg
~/ &
ls /var/cache/apt/archive/*.deb
share|improve this answer
the point is there are many parameter related between those tow parts of the script and i dont want to run it in background as i want the terminal which control it in front of me and thank u for replying , in case u have more explanation for me u are welcome :) – Ahmed Zain El Dein May 3 '13 at 21:48
you could pass the parameters to the other script ~/ val1 val2 &, unless you need them to change, if that is the case i dont know how – GM-Script-Writer-62850 May 3 '13 at 21:52
ok a part of this code above is a function can i put the secode while part in another file and execute it while executing can i call this function from inside the bashscript file of while it is within the other file with its definition and all ? – Ahmed Zain El Dein May 3 '13 at 23:07
the code i posted will run as its own process and not wait for it to finish before continuing, you can pass variables to it the same as you would anything else in bash – GM-Script-Writer-62850 May 4 '13 at 1:42
yes variable yes but i cant call within the file another function written in the other script file to execute it right ? – Ahmed Zain El Dein May 4 '13 at 12:40

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.