3

xt is a custom csh/tcsh alias to open shells with specific window colors. It issues the following command:

xterm -geometry 105x25 -font 8x13 -bc -bg #333 -fg #999 \!* &

How could one command be written to open a new shell with a color based on the level the shell is in a stack.

Example:

  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

While this is for csh/tcsh, bash answers are also acceptable if it’s easier/simpler there. As stated, I would like one command/alias to open the parent and child shells and decide what color to use.

0

2 Answers 2

4

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] \!* &"
3

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.

Demo:

> 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.

You must log in to answer this question.

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