问题
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:
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>"
.
来源:https://stackoverflow.com/questions/17885240/bash-ps1-only-executes-a-script-once-on-new-terminal-windows