bash ps1在新的终端窗口上仅执行一次脚本 [英] bash ps1 only executes a script once on new terminal windows

查看:185
本文介绍了bash ps1在新的终端窗口上仅执行一次脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.bashrc文件中有一个非常简单的ps1脚本,这会产生一个名为blah的随机脚本生成器.

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)** ::"

blah脚本每次仅应在shell中导出一个随机字符串....

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

但是,似乎每次我在iterm中创建一个新窗口时,该脚本才被执行,这当然意味着该脚本不再是随机的!

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 ::

我在这里做错什么了吗?刚开始学习bash,因此对缺乏术语深表歉意,随心所欲地编辑!

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

推荐答案

单引号/双引号ksh 中所述:

"$ PWD"立即解析. (...)当您设置为'$ PWD'时,它会 不会立即解决,因此在使用时会解决,并在以下情况下发生更改 您更改目录.

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.

所以改变

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

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

PS1='<code>'而不是PS1="<code>".

这篇关于bash ps1在新的终端窗口上仅执行一次脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆