#=============================================================================== @(#) favorite trick for putting non-printables in scripts Most machines now have printf(1) put this information may still be handy If you actually type the non-printable characters into a UNIX script you can cause problems if you copy the file to a screen or printer, as some of the sequences might cause actions on the devices. Some mail transmissions or editors may destroy or corrupt non-printable strings. The use of non-printable characters with echo(1) is not portable between various Unix implementations; and printf(1) is not always available. A very portable way of sticking the escape character into the appropriate variable that avoids this problem follows. As you can see, you get set up a lot of "non-printable" characters at once. ################# #!/bin/sh set -a # start automatically exporting variables in Bourne shell # no lowercase letters in first string given to tr(1) # make variable names in 'here' document all lowercase eval `tr '\[AM' '\033\001\015' <<\EOF esc=[ soh=A cr=M EOF ` set +a # stop automatically exporting variables in Bourne shell # you can now use the variables to generate 'nonprintable' strings. echo "${esc}2J" ################# Now, for example, the variable esc contains an actual escape character. esc is the octal value 033. Use lowercase for the variable names. Use uppercase for the letter to be converted so tr(1) does not accidently convert a letter in the variable name. I like to use the characters that would generate the desired character when used with the control key. UPDATE: Note that SOME echo(1) commands will let you put non-printable characters in them using \octal_codes or other special sequences; that the printf command can do this; #=============================================================================== Entering non-printable characters into a file using vi(1): To insert a non-printable character into a file or command when using vi, preceed the character with a control-V. Adding carriage returns and escapes to a map command in your .exrc files to define a function key is a common use of this feature. #=============================================================================== A safe way (other than a dump with od(1)) to display a file that might contain non-printable characters is to use the cat command with several options: cat -v -e -t On some machines -vet will work, on others it will not. On some machines the -v option must be present or the -t and -e options will not work. The most restrictive behavior is something like: -v Cause non-printing characters except tabs, new-lines and form-feeds to be printed. o Control characters are printed using the form ^X (Ctrl-X) o the DEL character (octal 0177) is printed as ^?. o Other non-printing characters are printed as M-x, where x is the character specified by the seven low order bits. When the -v option is used these options are also available: -e Print a $ character at the end of each line. This lets you see trailing white space, which sometimes is causing a problem. -t Print each tab character as ^I. On some systems (HP-UX, for one) commands called vis and inv are available for a somewhat similiar purpose. #===============================================================================