# Bash replacement to getchar

**URL:** <https://community.unix.com/t/bash-replacement-to-getchar/246929>\
**Category:** Shell Programming and Scripting\
**Created:** [August 13, 2009, 12:24am UTC](https://community.unix.com/t/bash-replacement-to-getchar/246929 "2009-08-13T00:24:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asafe](https://community.unix.com/letter_avatar/asafe/32/6_449c533f586939b6e5f246632a46421f.png) [@Asafe](https://community.unix.com/u/Asafe)\
**Post date:** [August 13, 2009, 12:24am UTC](https://community.unix.com/t/bash-replacement-to-getchar/246929/1 "2009-08-13T00:24:57Z")

</div>

There's a replacement in bash for getchar or get functions of C and C++?Those functions read the next char avaliable in the input stream.  
I've tried something like:

```nohighlight
OLD_STTY=`stty -g`
  stty cbreak -echo
  look=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
  stty $OLD_STTY

```

But it is not working very well.

---

<div class="post-metadata">

**Author:** ![kshji](https://community.unix.com/user_avatar/community.unix.com/kshji/32/15661_2.png) [@kshji](https://community.unix.com/u/kshji)\
**Post date:** [August 13, 2009, 2:48am UTC](https://community.unix.com/t/bash-replacement-to-getchar/246929/2 "2009-08-13T02:48:08Z")

</div>

Generic for all [shells](http://karjalanatk.awot.fi/sf/browser/showfile?cust=ka&subdir=koulutus/shell&doc=my_shell)

```plaintext
read -n1 kbd

```

If you not echo then use stty -echo before read and stty echo after.

Also this "old standard" works:

```plaintext
echo -n "Press key:"
stty -echo raw
c=$(dd bs=1 count=1 2>/dev/null )
stty echo -raw
echo " key:$c"

```

Ofcourse you can save current stty and return it, but usually echo is on and raw is off.

Ksh include KEYBD interrupt = you can trap KEYBD.

```plaintext
keybd()
{
    key="${.sh.edchar}"
}

trap 'keybd' KEYBD
while :
do
        read a
done

```

---

<div class="post-metadata">

**Author:** ![Asafe](https://community.unix.com/letter_avatar/asafe/32/6_449c533f586939b6e5f246632a46421f.png) [@Asafe](https://community.unix.com/u/Asafe)\
**Post date:** [August 13, 2009, 3:43pm UTC](https://community.unix.com/t/bash-replacement-to-getchar/246929/3 "2009-08-13T15:43:06Z")

</div>

```nohighlight
echo -n "Press key:"
stty -echo raw
c=$(dd bs=1 count=1 2>/dev/null )
stty echo -raw
echo " key:$c"

```

It interpreters ctrl-c and ctrl-d literally... Is there a way to make it to be aborted by a signal?

---

<div class="post-metadata">

**Author:** ![kshji](https://community.unix.com/user_avatar/community.unix.com/kshji/32/15661_2.png) [@kshji](https://community.unix.com/u/kshji)\
**Post date:** [August 14, 2009, 2:23am UTC](https://community.unix.com/t/bash-replacement-to-getchar/246929/4 "2009-08-14T02:23:34Z")

</div>

With this you can debug:

```nohighlight
trap 'echo "int shell"' HUP INT QUIT
echo -n "Press key:"
stty -echo raw
c=$(dd bs=1 count=1 2>/dev/null )
stty echo -raw
echo "<$c>"
echo $c | od -c

echo "press ctrl-c"
sleep 20

```

result: dd read one char from stdin. Also ctrl-c and ctrl-d are only chars. So you can test those chars.

```nohighlight
trap ':' HUP INT QUIT
c=""
while ["$c" = ""]
do
  # do c read, look previous dd or
  echo -n "key:"
  read -n 1 c 
  # test it
  case "$c" in
    \001) ;; # ctrl-A
    \003) ;; # ctrl-C
  esac
  # remove char ascii 01-04
  c=$(echo "$c" | tr -d "[\001-\004]" )
done

```
