4

In Bash the only way to get a (user) input seems to be to use the read method, which pauses the rest of the script. Is there any way to receive a command line input (ending with the enter key) without pausing the script. From what I've seen there may be a way to do it with $1 ..?

9
  • It can probably be done but at this point you should stop using a bash script and switch to something more powerful. Personally I'd use C, but perhaps Perl would be a better choice. Commented Jan 5, 2015 at 2:35
  • 2
    What are you intending to do with the input once you've received it? Commented Jan 5, 2015 at 2:36
  • $1, $2, ... are the script arguments. Commented Jan 5, 2015 at 2:42
  • 2
    This definitely has the feel of being an XY Question. Commented Jan 5, 2015 at 2:48
  • 1
    @AbhiBeckert, you can launch a command in background with &, but as @Etan said, whatcha gunna do with it? Commented Jan 5, 2015 at 2:52

1 Answer 1

5

read -t0 can be used to probe for input if your process is structured as a loop

 #!/bin/bash

 a='\|/-'
 spin()
 {
  sleep 0.3
  a="${a:1}${a:0:1}"
  echo -n $'\e'7$'\r'"${a:1:1}"$'\e'8
 }

 echo 'try these /|\- , dbpq , |)>)|(<( , =>-<'

 echo -n "  enter a pattern to spin:"
 while true
 do
   spin
   if read -t0 
   then
     read a
     echo -n "  using $a enter a new pattern:" 
   fi
 done

else you could run one command in the background while promptiong for input in the foreground. etc...

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for cool spinning patterns :). Check this one too: |)>=<(|(<=>)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.