Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I currently use an alias in csh/tcsh to open shells with specific window colors.
Example: xterm -geometry 105x25 -font 8x13 -bc -bg #333 -fg #999 \!* &

What I'd like to do is have one command open a new shell with a color based on the level the shell is in a tree.


  1. %> xt
    Open shell A (white bg/black fg) - new shell
  2. %> xt
    Open shell A.a (black bg/grey fg) - child shell
  3. %> xt
    Open shell A.a.a (green bg/yellow fg) - grandchild shell

I have bash available, but I use tcsh or csh by default.
As stated, I would like one command/alias to open the parent and child shells and decide what color to use.

share|improve this question

migrated from Jul 28 '10 at 14:20

This question came from our site for professional and enthusiast programmers.

What's the perl tag for? – Dennis Williamson Jul 28 '10 at 22:59

Most shells (tcsh, bash, zsh, but not csh) increment the SHLVL environment variable when they start, so that $SHLVL indicates the level of nesting of your current shell. So do something like (for tcsh)

set xt_bg_colors=(white white black green)
set xt_fg_colors=(black black gray yellow)
alias xt "xterm -geometry 105x25 -font 8x13 -bc -bg $xt_bg_colors[$SHLVL] -fg $xt_fg_colors[$SHLVL] \!* &"
share|improve this answer

I would create an array variable with the list of color specifications in it (or two arrays - one for fg and one for bg). Then, in your alias select from that array using an index variable, then increment it.


> set array=(apple banana cherry)
> @ index = 1
> @ index = ( ( $index ) % ${#array} + 1 ) ; echo $array[$index]

Each time you execute the last line (which represents your alias), the current item in the array is echoed, the index is incremented and then the index wraps around if it exceeds the length of the array.

You may need to use a file to keep track of the index instead of an in-memory variable.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .