bash ps1 only executes a script once on new termin

2019-07-17 03:20发布

This question already has an answer here:

I have a very simple ps1 script in my .bashrc file, this excutes a random script generator called blah.

export PS1="\[$(tput bold)\]\[$(tput setaf 1)\]\\$  \[$(tput sgr0)\] $PWD :: **$(. blah)** ::"

the blah script simply should export a random string each time in the shell....

function silly {
     local RANDOMQUOTE=$[ ($RANDOM % 10 ) ] local fooey=('rand1' 'rand2' 'rand3' 'rand4' 'rand5'); 
     echo ${fooey[RANDOMQUOTE]}
}

silly

However it seems this script is only excuted each time I create a new window in iterm, and of course this means the script is no longer random!

Example of the random function not being called each time:

$   /Users/username :: rand2 ::
$   /Users/username :: rand2 ::
$   /Users/username :: rand2 ::
$   /Users/username :: rand2 ::

Am I doing something wrong here? Just starting to learn bash so apologies for lack of terminology, edit at will!

1条回答
何必那么认真
2楼-- · 2019-07-17 04:15

As said in Single/double quotes ksh:

The "$PWD" resolves immediately. (...) When you set to '$PWD', it does not resolve immediately, so it resolves when used, and changes when you change directories.

So change

export PS1="[$(tput bold)][$(tput setaf 1)]\$ [$(tput sgr0)] $PWD :: $(. blah) ::"

for

export PS1='[$(tput bold)][$(tput setaf 1)]\$ [$(tput sgr0)] $PWD :: $(. blah) ::'

That is, PS1='<code>' instead of PS1="<code>".

查看更多
登录 后发表回答