tput and stty

Both of these have to do with your terminal. Either can be informational (telling you something about your terminal settings) or active (changing something). For example, "tput" can control the position of the screen cursor:

Try this:

tput clear
echo "hello world"
echo "Input please"
tput cuu1
tput cuu1
tput cuf1
tput cuf1
tput cuf1
tput cuf1
tput cuf1
tput cuf1
tput el
read akey
tput clear
echo Thanks

You can look up the things you can do in "man terminfo". For example, "tput clear" will clear the screen, while "tput cols" will tell you how wide your terminal is.

Another useful resource is Colours and Cursor Movement With tput.

On the other hand, stty controls things like "echo". Let's say that you wanted to turn off character echo while reading a password. You could do this:

old=`stty -g`
stty -echo
read password
stty $old

Why save the old stty settings? It's so you can put them back to what they were. If you just said "stty echo". you are assuming that's how it was previously set. It might not be: if the script was called from an emacs session, echo would have been off and you would have turned it back on!

You can set colors: Using color in shell scripts (Linux, Mac OS X), switch to a line drawing characer set and much more.

A simple "stty sane" can often restore a garbled screen (see My screen is completely unreadable if that's not enough).

You might also try "tput reset" and "tput init" (which may be indentica;l see "man tput").

You can fix backspace confusion that comes up when telneting from Linux to SCO:

   case $TERM in
            stty intr '^c'
            stty erase '^?'
            tput smpch
    export TERM

(From Problem with backspace key when telnet from Linux)

A common confusion for folks unfamiliar with SCO is that, by default, SCO systems set interrupt to the DELETE key, not CTRL-C. If you find that unbearable, you can easily change it; for example,

 stty intr ^C

will change your interrupt on Bourne or Korn shells. In this example, you actually type CTRL-C; if you are in vi adding it to your .profile, type CTRL-V and then CTRL-C

This can also be added to .profile (or whatever startup file your shell uses) or to /etc/profile.

We'd use stty "hold-open" scripts to control settings on serial printers also.

Flow control is also controlled by stty. That's often needed for serial sevices, but even on virtual terminals you can pause output with Cntrl-S and (if stty ixany is set), restart it with any keystroke.

Should you happen to be using a serial terminal for some reason, Linux Serial How-To may be helpful. Real serail terminal or not, "stty -a" will display its settings:

$ stty -a
speed 9600 baud; 35 rows; 119 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
tony@Ant-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
	-ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
	-dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
	eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
	min = 1; quit = ^\; reprint = ^R; start = ^Q; st

The 9600 baud is meaningless unless you really are on serial.

Serial Wiring and Communications for the Confused might also be useful if you find yourself dealing with some ancient serial system.

See also Terminfo Capabilities and Termcap and Terminfo.

Got something to add? Send me email.

Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Anthony Lawrence

Kerio Samepage

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us