4

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
batsta13
  • 549
  • 2
  • 12
  • 26
  • 3
    There is no way for a child process to change its parent process without cooperation from the parent. Your example suggests that you are primarily trying to figure this out. If that is the case, you are done now. If there is a problem you need to solve, perhaps you can arrange for the parent to cooperate, like `PS1=$(subshell commands ...)` – tripleee Apr 03 '12 at 10:25
  • You should specify what exactly youre trying to accomplish, not how youre trying to accomplish it. That way we can better understand what youre trying to do, and help with a correct solution. – phemmer Apr 03 '12 at 11:43
  • I wish to change the command prompt to "Hello World >" but if possible I don't want it to be permanent – batsta13 Apr 03 '12 at 11:50

3 Answers3

6

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
Alfe
  • 56,346
  • 20
  • 107
  • 159
1

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

Ryan
  • 2,755
  • 16
  • 30
0

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\]'"

enter image description here

Frank N
  • 9,625
  • 4
  • 80
  • 110