bash ps1 only executes a script once on new terminal windows [duplicate]

人盡茶涼 提交于 2019-12-11 02:17:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!