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

2 Answers 2


This is how I do that:



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

while true; do
    read -t 1 -n 1 bb && ACTION
    echo -ne "Hello $a times. Hit \"q\" to quit."'\r' 
    a=`echo "$a + 1" | bc`
exit 0

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
  • Calling READ from READ adds a stackframe. If you do that too much you get a segfault: re() { t=$((t+1)); if [[ $t -gt 80000 ]]; then echo foo; return; fi; re; }; re
    – Ole Tange
    Apr 9, 2019 at 8:57
  • You are right. I've updated the script with "while true". Thank you!
    – desgua
    Apr 12, 2019 at 16:00

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

cat /var/log/dmesg
~/inf-loop.sh &
ls /var/cache/apt/archive/*.deb
  • 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 :) May 3, 2013 at 21:48
  • you could pass the parameters to the other script ~/inf-loop.sh val1 val2 &, unless you need them to change, if that is the case i dont know how May 3, 2013 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 loop.sh while it is within the other file with its definition and all ? May 3, 2013 at 23:07
  • the code i posted will run loop.sh 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 May 4, 2013 at 1:42
  • yes variable yes but i cant call within the loop.sh file another function written in the other script file to execute it right ? May 4, 2013 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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