您能将 .bashrc 类型的命令限制到单个终端配置文件吗?

您能将 .bashrc 类型的命令限制到单个终端配置文件吗?

作为 Git 在线课程的一部分,导师建议我们在 .bashrc 文件末尾添加一些命令,以便终端提示符显示一些有用的 Git 相关信息。这些更改对于 Git 课程非常有用,但我不喜欢将它们用于一般用途。

Gnome 终端能够创建配置文件并设置每个配置文件的首选项。

是否可以这样配置 Bash,使得添加到 .bashrc 文件的更改仅与单个终端配置文件关联?我希望在一个终端配置文件中显示与 Git 相关的信息,而不显示在其他终端配置文件中。

如果相关的话,这是 .bashrc 文件末尾的代码:

# Enable tab completion
source ~/git-completion.bash

# colors!
green="\[\033[0;32m\]"
blue="\[\033[0;34m\]"
purple="\[\033[0;35m\]"
reset="\[\033[0m\]"

# Change command prompt
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# '\u' adds the name of the current user to the prompt
# '\$(__git_ps1)' adds git-related stuff
# '\W' adds the name of the current directory
export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"

答案1

创建单独的运行命令(rc)文件以.bashrc

保持~/.bashrc原样。打开终端后,您将可以进行正常操作。

您想附加以下行以~/.bashrc放入新文件中:

# ~/.gitrc
# Call after opening terminal use ". .gitrc" when it is time to
# work on Git course labs.

# Enable tab completion
source ~/git-completion.bash

# colors!
green="\[\033[0;32m\]"
blue="\[\033[0;34m\]"
purple="\[\033[0;35m\]"
reset="\[\033[0m\]"

# Change command prompt
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# '\u' adds the name of the current user to the prompt
# '\$(__git_ps1)' adds git-related stuff
# '\W' adds the name of the current directory
export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"

cd ~/gitdir

在两个世界之间切换

打开终端后,如果您希望 git 提示,请使用:. .gitrc

运行后. .gitrc,如果您希望在同一个终端会话中使用常规提示,只需使用Ctrl+ Shift+ T。将打开一个新的终端选项卡,其中包含常规提示,~/.bashrc而无需~/.gitrc修改。

不要使~/.gitrc可执行文件

无需使用chmod a+xon ~/.gitrc,因为您正在“获取”它。这意味着它在当前 shell 中运行,而不是作为必须标记为可执行的子 shell 进程运行,但也意味着变量在运行后消失。

本质上,将其标记~/.gitrc为可执行文件,然后使用.gitrc而不是 来调用它. .gitrc会破坏它。保留~/.gitrc常规文件可确保您不会意外地将其作为命令执行。

相关内容