Unix Tip: Using stty to Your Advantage

By Sandra Henry-Stocker, ITworld.com |  Operating Systems Add a new comment

Send in your Unix questions today!


See additional Unix tips and tricks



The stty (set the options for a terminal) command can be very useful once you get the knack of using it. Stty can give you quick manual control over the characteristics of your terminal. If ever you have found yourself working on a terminal in which the terminal settings did not match your actual terminal, you will know how annoying this situation can be. It is often experienced when you attempt to backspace to fix a typo on the command line and, instead of seeing the cursor move backwards on the line erasing characters, you see an increasingly long string of ^H or ^? characters appearing at the end of your command line.


boson> echo "Please send me the lsit^H^H^H


What this tells you is that ^H (control-H) is not functioning as an erase. Using stty, however, you can reset your erase character like this:

boson> stty erase \^h



To see a list of your current stty settings, type the following command:

stty -a



This command will list your current settings in a format like this:


boson> stty -a

speed 38400 baud;

rows = 80; columns = 132; ypixels = 0; xpixels = 0;

csdata ?

eucw 1:0:0:0, scrw 1:0:0:0

intr = ^c; quit = ^\; erase = ^h; kill = ^u;

eof = ^d; eol = <<undef> eol2 = <undef> swtch = <undef>

start = ^q; stop = ^s; susp = ^z; dsusp = ^y;

rprnt = ^r; flush = ^o; werase = ^w; lnext = ^v;

-parenb -parodd cs8 -cstopb -hupcl cread -clocal -loblk -crtscts -crtsxoff -parext

-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -iuclc

ixon -ixany -ixoff imaxbel

isig icanon -xcase echo echoe echok -echonl -noflsh

-tostop echoctl -echoprt echoke -defecho -flusho -pendin iexten

opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel tab3



The standard control characters are:

control-d end of file (eof)

control-h backspace (erase)

control-z suspend (susp)

control-u delete text from start of line (kill)

control-c interrupt



The most commonly used stty command is undoubtedly the "stty erase ^h" command which is often used in scripts


if [ `tty | grep -ci console` -eq 0 ]
then
	stty ERASE ^H
	TERM=SUN
fi

You can also display your settings in a format which allows you to restore them at a later time. This is done with the "stty -g" command, the output of which (on one of my Solaris systems) looks like this:


boson> stty -g

2502:1805:f00bf:8a3b:17:1c:7f:15:4:0:0:0:11:13:1a:19:12:f:17:16:0:0:1:1:0:00:0

:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0


If you want to save and restore your terminal settings, you can use the output of the stty -g command to create a command that will do this for you. First, let's examine what the command would look like:



boson> echo stty `stty -g`

stty 2502:1805:f00bf:8a3b:3:1c:f:15:4:0:0:0:11:13:1a:19:12:f:17:16:0:0:1:1:0:00

:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0


The "stty 2502:1805 ..." command can be used to restore the current settings at a later time. So, let's store that command in a file and make it work as a script:


boson> echo stty `stty -g` > restore-tty; chmod 755 restore-tty


Now, let's test the script by first resetting our erase character from ^H to ^A and then restoring it back to its previous setting using our script.


boson> stty erase \^a

boson> echo This is a mess^H^H^H

This is a mess

Obviously, control-H isn't working any longer as a backspace character, though control-A should work just fine. To restore the previous settings, we invoke our script:


boson> ./restore-tty



Control-H should now work as before.



If you want to change your stty settings in a script and then restore them, you don't have to build a script to preserve the initial settings. You can save your settings in a variable and then use the stty command to restore from that:

oldSettings=`stty -g`			# save stty settings
stty -echo raw
<other commands>
stty $oldSettings 			# restore stty settings

Other stty settings allow you to change the usable size of your terminal. For example, if you need a narrower display, you could set your terminal width to be narrower by setting your columns from the normal 80 to something more narrow:



stty columns 60



If you list your files after running this command, you will note the changes in your display.

    Add a comment

    Post a comment using one of these accounts
    Or join now
    At least 6 characters

    Note: Comment will appear soon after you have activated your account.
    Obscene/spam comments will be removed and accounts suspended.
    The information you submit is subject to our Privacy Policy and Terms of Service.

    ITworld LIVE

    Operating SystemsWhite Papers & Webcasts

    White Paper

    Microsoft Enterprise Agreement Program Overview

    Discover how flexible the Microsoft Enterprise Agreement Program is to help you build the right software solution agreement for your business. This paper highlights all the available options-from on-premise software and cloud service solutions, to payment options and enrollment programs, and more.

    White Paper

    Watson - A System Designed for Answers. The future of workload optimized systems design

    Watson is a workload optimized system designed for complex analytics, made possible by integrating massively parallel POWER7 processors and DeepQA technology. Read the white paper about Watson's workload optimized system design.

    See more White Papers | Webcasts

    Ask a question

    Ask a Question