getchar - Linux


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.


getchar [-c] [-]


  • -c: Disable line buffering by ignoring the value of the LC_ALL, LC_CTYPE, or LC_TERMINAL environment variables.
  • -: Disable echo by suppressing the display of the entered character.


Read a single character:

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.


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.