问题
Using a script, I was to change the prompt of the parent Bash shell. I have tried the following:
PS1="Hello World > "
This changes the prompt of the subshell, which the script is running in, but which command would I use to change the prompt of the parent shell. Any ideas?
回答1:
In all cases the parent shell must cooperate. The child process in a unix environment cannot influence the parent process without its cooperation.
Try this in the subshell script changePrompt.sh
:
echo 'PS1="Hello World > "'
And then call the script from the parent shell like this:
eval "$(changePrompt.sh)"
Or, a different approach: Source the script instead of calling it. changePrompt.sh
:
PS1="Hello World > "
Call it like this:
source changePrompt.sh
or simply:
. changePrompt.sh
回答2:
you have to edit the .bash_rc file, with what you want... just straight up add PS1="blah" or whatever.
the .bash_rc file should be in your home dir /user/home or whatever (its hidden so "ls -la")
when you have edited it, source it, and it should work (source .bash_rc) -- assuming same dir
if that doesnt work try the .rc file.... this is system wide though for all shells (or at least it should be)..... try here for more info:
http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html --- here
回答3:
Aliases (in your ~/.bashrc
or ~/.bash_aliases
) are also a good way, if it's just about conveniently changing your prompt now&then...
alias miniprompt="PS1='\[\e[32;1m\]$>\[\e[0m\]'"
alias fullprompt="PS1='\u\[\e[34;1m\]@\[\e[36;1m\]\H \[\e[34;1m\]\w\[\e[32;1m\] $ \[\e[0m\]'"
来源:https://stackoverflow.com/questions/9991116/changing-ps1-prompt-in-a-bash-parent-shell