1

I'm trying to make a custom prompt that selects one random value from the following lists:

color=(1 2 3 4 5 6)
much=(such very much many so)
wow=(wow hacker terminal geek confusion)

then does this before every prompt.

tput setaf $rcolor
echo -n "$rmuch $rwow $ "

The problem is, if I use PS1, it executes on terminal startup and doesn't update before every prompt. How do I get a different, random message before every prompt?

Sample output:

such hacker $
much wow $
very geek $
such wow $
much confusion $
very terminal $
clapp
  • 195
  • 3
  • Almost a duplicate of http://stackoverflow.com/questions/4585397/bash-run-some-command-before-or-after-every-command-entered-from-console – Diego Basch Oct 24 '15 at 06:09

2 Answers2

2

You have to escape the $:

PS1="\$(tput setaf \${color[\$RANDOM%6]})\${much[\$RANDOM%5]} \${wow[\$RANDOM%5]}: "

PS1 is evaluated every time is printed.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

You can use trap as well:

much=(such very much many so)
wow=(wow hacker terminal geek confusion)
max=${#much[@]}
trap 'PS1="${much[$((RANDOM % max))]} ${wow[$((RANDOM % max))]} $ "' DEBUG

Output:

much confusion $
such terminal $
such confusion $
such geek $
so terminal $
very confusion $
such geek $
such terminal $
much terminal $
many geek $
anubhava
  • 761,203
  • 64
  • 569
  • 643