Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set read command to execute without having to press Enter button in shell?

Tags:

shell

unix

I use the below code to assign a given by the user character to variable G:

read G

However in order to move forward with executing of my script I need to press enter button. Is it possible to set the read command somehow that it show process forward to the next line immediately after it receive the one letter from stdin?

like image 608
goe Avatar asked Nov 29 '09 18:11

goe


People also ask

How do you run a shell script without changing permission?

For shell scripts without execute permissions, you will need to pass the script as an argument to your choice of interpreter. Here the execute permissions are checked for your shell interpreter and not the script itself. So you should have execute for /bin/sh and your script can have only read permissions.

What is use of the read command in shell?

The read command reads one line from standard input and assigns the values of each field in the input line to a shell variable using the characters in the IFS (Internal Field Separator) variable as separators.


1 Answers

If you're using Bash:

read -n1 G

Your system is configured to use dash by default and dash doesn't support this feature, so you also need to change the first line of your script to specify that you wish you use bash:

#!/bin/bash
like image 182
Mark Byers Avatar answered Nov 15 '22 05:11

Mark Byers