Frage

I was wondering if there is any way to have a prompt that change every time i press Enter, like:

[jtouzea - such prompt]  $>
[jtouzea - much style]   $> ls -l
total 0
drwx------+  5 jtouzea  2013  170 Mar 11 16:50 Desktop
drwx------+  2 jtouzea  2013   68 Mar 11 16:49 Documents
drwx------+  2 jtouzea  2013   68 Mar 11 16:58 Downloads
drwxr-xr-x@ 26 jtouzea  2013  884 Mar 11 17:20 Library
drwx------+  2 jtouzea  2013   68 Mar 11 16:50 Movies
drwx------+  2 jtouzea  2013   68 Mar 11 16:50 Music
drwx------+  2 jtouzea  2013   68 Mar 11 16:50 Pictures
drwxr-xr-x+  2 jtouzea  2013   68 Mar 11 16:50 Public
[jtouzea - wow]          $> echo "test"
test
[jtouzea - 10/10]        $>

i have already found the $RANDOM command, that allow to do so, but i need to do:

source ~/.zshrc

or else i doesn't change my prompt.

Any idea?

EDIT: Currently i have:

PROMPT="[jtouzea - $RANDOM] $> "

in my .zshrc

EDIT2: I use zsh, so unfortunately, PROMPT_COMMAND doesn't seems to work

EDIT3: Here is my final code now that the question is solved:

function precmd()
{
    sentence[1]="much prompt";
    sentence[2]="such style";
    sentence[3]="wow";
    nb=$[$RANDOM % 3 + 1];
    PROMPT="[jtouzea - ${sentence[$nb]}] $> ";
}
War es hilfreich?

Lösung

Try using the PROMPT_COMMAND hook:

PROMPT_COMMAND() { randPromptNum=$[ $RANDOM % 3 ]; PS1=${POSSIBLE_PROMPTS[$randPromptNum]};}

For zsh, you must use precmd instead of PROMPT_COMMAND.

Before you use this, you must set up a variable with all possible prompts:

POSSIBLE_PROMPTS[0]="such prompt: "
POSSIBLE_PROMPTS[1]="much style: "
POSSIBLE_PROMPTS[2]="wow: "

If you want to adjust the number of prompts possible, remember to edit the % 3.

EDIT - Result on cygwin

much style: echo hi
hi
such prompt: echo blah
blah
much style: pwd
/usr/bin
such prompt: yes y | head
y
y
y
y
y
y
y
y
y
y
wow: echo foo
foo
such prompt: 

Andere Tipps

It's not the same shell as yours, but you could try this:

I edited my .kshrc file like this:

PS1="Look, it's random: \$RANDOM  \$ "

And then sourced the file, and pressed Enter several times:

/home/user  $ . .kshrc
Look, it's random: 2155  $
Look, it's random: 6032  $
Look, it's random: 13065  $

Put all your phrases in a text file or an array and then use $RANDOM to look into that.

Note: remember to escape the $.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top