0

(Using zsh) I am just trying to get random color output whenever I open a new tab in my terminal. To achieve this I wrote the following shell script, but it is not working as expected:

#Standard Colors
red='\033[0;31m'
NC='\033[0m' # No Color
black='\033[0;30m'
blue='\033[0;34m'
green='\033[0;32m'
cyan='\033[0;36m'
purple='\033[0;35m'
yellow='\033[1;33m'
lgreen='\033[1;32m'
lblue='\033[1;34m'
lred='\033[1;31m'
lcyan='\033[1;36m'
#Array color
colr=(red blue green cyan purple yellow lgreen lblue lred lcyan)

#Get random number for colors
randcolr()
{
  sz=${#colr[@]}
  randval=$(( ( RANDOM % sz )  + 1 ))
  echo "${colr[randval]}"
}

echo -e "$(randcolr)TESTING"

Instead of getting colored output, it wrote the name of color and "TESTING" e.g. lgreenTESTING. Any help?(I am using zsh)

The function randcolr always gives same value inside a terminal.

0x6773
  • 1,116
  • 1
  • 14
  • 33

1 Answers1

2

When you define your array, you are using strings instead of variables:

colr=(red blue green cyan purple yellow lgreen lblue lred lcyan)

Should be:

colr=($red $blue $green $cyan $purple $yellow $lgreen $lblue $lred $lcyan)
Josh Jolly
  • 11,258
  • 2
  • 39
  • 55
  • Alternatively, use `${(P)$(randcolr)}` to expand the variable named by the output of `randcolr`. – chepner May 03 '15 at 19:13