getchar - Linux
Overview
getchar is a command-line utility that reads a single character from the standard input. It is primarily used in scripts and programs to obtain user input or handle interactive scenarios.
Syntax
getchar [-c] [-]
Options/Flags
- -c: Disable line buffering by ignoring the value of the
LC_ALL
,LC_CTYPE
, orLC_TERMINAL
environment variables. - -: Disable echo by suppressing the display of the entered character.
Examples
Read a single character:
character=$(getchar)
echo "Entered character: $character"
Disable echo:
getchar - echo "Entered character: $(getchar -)"
Common Issues
- Input blocking: getchar will block the execution of the script until a character is entered. To avoid blocking, consider using
read -n1
instead.
Integration
Combining with other commands:
- tr: Convert the entered character to uppercase:
getchar | tr '[a-z]' '[A-Z]'
- grep: Filter the entered character based on a pattern:
getchar | grep [pattern]
Related Commands
- read: Read a complete line of input from the standard input.
- getkey: Read a single keystroke, including function keys and arrow keys.
- stty: Set terminal I/O parameters, including echo and input buffering.