Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read just a single character in shell script

Tags:

linux

shell

io

ksh

I want similar option like getche() in C. How can I read just a single character input from command line?

Using read command can we do it?

like image 480
footy Avatar asked Jan 04 '12 11:01

footy


People also ask

What is %s in shell script?

%s is a format specifier for printf command. Using the format string %s causes the arguments to be concatenated without intervening spaces. It interprets the associated argument literally as string. Joe Smith. Knows FORTRAN, 36-bit MACRO assembler, Perl, and Bash.

What is $? == 0 in shell script?

$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded. The grep manpage states: The exit status is 0 if selected lines are found, and 1 if not found.

What is $@ and $* in shell script?

• $* - It stores complete set of positional parameter in a single string. • $@ - Quoted string treated as separate arguments. • $? - exit status of command.

What does $() mean in shell?

Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source). Connect RDS to Lightsail.


1 Answers

In bash, read can do it:

read -n1 ans 
like image 152
SiegeX Avatar answered Oct 06 '22 06:10

SiegeX