2

I'm new to AIX and I miss some tricks that work well on other *nix flavors.

I need a CTRL sequence in a ksh scripts, like ^[ (CTRL-[) and to do that I'm habit to use the ctrl-v [ , but here it doesn't work.

At the moment I'm obliged to use a windows box with putty so I cannot even edit the scripts on my Linux box and transfer the scripts on the AIX server.

Do you know why and how I can fix the issue?

To resume the answers:

@Dennis:

there are a few other ways to use escape in a Korn shell script:

print '\E' escape1='\033'    # contains the literal characters as shown
echo -e "$escape1"  
printf '%b' "$escape1"  
print "$escape1"
escape2=$'\e'     # contains an actual escape  
echo "$escape2"  
printf '%s' "$escape2"  
print "$escape2"  

For the terminal colors

man 5 terminfo

$'' notation allow ANSI-C escaping:

green=$'\e[01;32m'

It may be that ksh88 don't support $''

I use a ksh88, but I can switch to ksh93.

ctrl-v on command line
Version M-11/16/88i 

/usr/dt/bin/dtksh
print ${.sh.version}
Version M-12/28/93d

green=$'\e[01;32m' doesn't give me any error on ksh88,

but it doesn't expand the escape sequence. On ksh93 $'' the same issue

green="$'\e[01;32m'"

Using uppercase E solved the issue (weird!!!):

print $'\E[01;32m hello'

To summarize:

\E  works
\e  NO
\033    works just with echo
^[     ^v^[ do not work at all
chicks
  • 3,639
  • 10
  • 26
  • 36
tmow
  • 1,187
  • 8
  • 20

2 Answers2

3

Try this:

stty -a

and see if lnext is ^V. If not, try:

stty lnext ^V

where you will type "^" (caret) and "V" as separate characters. Now try to see if you can type an escape using ^V^[ (does ^VEsc work on your keyboard?).

Also, check to see if you're in emacs mode

Depending on how you're using it, there are a few other ways to use escape in a Korn shell script:

print '\E'
escape1='\033'    # contains the literal characters as shown
echo -e "$escape1"
printf '%b' "$escape1"
print "$escape1"
escape2=$'\e'     # contains an actual escape
echo "$escape2"
printf '%s' "$escape2"
print "$escape2"

Of course, you wouldn't normally output escape all by itself.

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
  • Good point Dennis, lnext was set correctly but I was in emacs mode. So I solved the issue as you advised as follow: blue="\\033[01;32m" green="\\033[01;34m" cyan="\\033[01;36m" white="\\033[0m" export PS1="$(echo ${blue})${USER}@$(echo ${green})${HOST%%.*} $(echo ${cyan})${PWD##*/} $(echo ${white}) " THANKS A LOT! – tmow Dec 27 '10 at 16:22
  • @antenore: If you use the `$''` form, you won't have to use all those `echo` commands: `blue=$'\e[01;32m'`, etc., and `PS1="$blue$USER@ ...` (except that on my system 32 gives me green and 34 gives me blue). Ctrl-V should work in emacs mode, btw, but it's better to use a representative form of control characters instead of a literal form. Another way to set up your colors: `blue=$(tput setaf 4)` (see `man 5 terminfo` for a list of color numbers). – Dennis Williamson Dec 27 '10 at 19:26
  • @Dennis tnx, I've tried it but it doesn't work and I cannot find out why. The only way to use escape sequences inside a VAR is to use the echo... That's really weird. Well my problem is solved, but not my need to know. – tmow Dec 28 '10 at 10:18
  • Does `blue=$'\e[01;32m'` give you an error message? It may be that you're running ksh88 and it may not support `$''`. Or is something else happening? – Dennis Williamson Dec 28 '10 at 14:58
  • So, blue is green, sorry for the mistake. I'm doing all the test on a Solaris host at the moment, anyway I've the same issues. I use a ksh88, but I can switch to ksh93. Version M-11/16/88i (ctrl-v on command line). /usr/dt/bin/dtksh print ${.sh.version} Version M-12/28/93d green=$'\e[01;32m' doesn't give me any error on ksh88, but it doesn't expand the escape sequence. On ksh93 $'' the same green="$'\e[01;32m'" – tmow Dec 29 '10 at 08:00
  • @antenore: Part of your comment apparently got cut off. If you're using double quotes around it, too, it's not going to work. – Dennis Williamson Dec 29 '10 at 09:12
  • On ksh93 with $'' I have the same issue, it prints just the code inside the parenthesis. So far the first solution is the only one the works as expected. – tmow Dec 29 '10 at 09:48
  • Weird, but this is the solution: print $'\E[01;32m hello' – tmow Dec 29 '10 at 09:56
2

If I recall with AIX, the Ctrl+V escape sequence should work, however I remember too that the default TERM in AIX wasn't always what you would expect. Check your $TERM variable. If it's weird:

export TERM=xterm  

or

export TERM=vt100

If your $TERM is wrong, then even if Ctrl+V works, Ctrl+[ might not do anything. It's a VT100 sequence.

wittich
  • 147
  • 1
  • 10
mgjk
  • 854
  • 3
  • 9
  • 19
  • I thought was that as well, but no luck. I found that to do the ^V I must hold the ctrl also while I'm pressing the V, for the [ it still doesn't work. I have a Swiss keyboard so to digit the [ I need to hold the alt-gr key – tmow Dec 27 '10 at 12:18
  • the keystroke should be ctrl-v ctrl-[, I just tried it with the german keyboard layout and it doesn't work for me either(!). Switching back to the EN(US) layout, I can type the combination okay. (the German combination for ctrl-[ being ctrl-altgr-8 ) – mgjk Dec 27 '10 at 14:53
  • In the swiss qwertz keyboard the [ is on the left of the p (with è and ü holding the alt-gr), still the same issue... I think I'll add a second keyboard with a US layout... much better to work on *nix environments. – tmow Dec 28 '10 at 13:23