8

I'm trying to create a simple script that, among other things, will create a subshell:

#!/bin/sh

# setup

"${@:-$SHELL}"

# teardown

The question is: I need to change the default prompt, so for example:

$ # default shell
$ ./myscript
(myscript) $ # subshell
(myscript) $ exit
$

I tried to change PROMPT and PS1, but none of these works. How can I do that?

PS.: I need a solution that works both on Bash and ZSH, if possible.

7

I think you can create a subshell with a different prompt like this:

$ bash --rcfile <(echo "PS1='subshell prompt: '") -i

Example

Current env:

$ bash --rcfile <(echo "PS1='subshell prompt$ '") -i

In sub shell:

subshell prompt$ echo hi
hi
subshell prompt$ exit
exit

Back to original shell:

$ 
  • 1
    You can use a here string instead of the process substitution. eg <<< "PS1...". – jordanm Aug 17 '13 at 1:25
  • 1
    @jordanm - can you provide an example? I messed around with this but couldn't get a working example using the here strings. – slm Aug 17 '13 at 4:16
2

The reason it doesn't work might be because when you load the shell (which should be eg.: /bin/bash), it will end up reloading its configuration files, which include the environment variables for the prompt (eg.: $HOME/.bashrc) after being passed the environment variables you want - thus your variables are discarded.

You might want to check the options of the shell to load custom configuration files (eg.: --rcfile in Bash IINM) and use those as well as \env to pass the environment variables that you want. The problem is that once you do that firing the subshell becomes shell-dependant.

(CAVEAT EMPTOR: I do not consider this to be an answer yet - in particular, I have not tried any of this in my system yet.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.