简体   繁体   中英

Run new zsh and bash shells with custom as command line agrument

Is it possible to run new zsh or bash shell with custom PS1 set from command line? It should overwrite the default theme set by .bashrc and .zshrc respectively.

I'm talking about something like zsh --myprompt="yes master? >"

EDIT: I do not want to affect any user-side configuration files. I want it to work for any user with any configuration.

Create your own "shim" rcfile that's available to your users then call that with the --rcfile option (for bash) or --rcs option (for zsh). This should source the user's rcfile first. For example, let's call this /usr/local/share/.fancypromptrc. In bash this might look like:

source "$HOME/.bashrc"
export PS1="DOLLAZ $"

And in zsh this might look like:

source "${ZDOTDIR:-$HOME}/.zshrc"
export PS1="DOLLAZ $"

Then the user would start bash with bash --rcfile /usr/local/share/.fancypromptrc . In zsh it would be zsh --rcs /usr/local/share/.fancypromptrc .

This way the user doesn't have to modify their rcfile and if they are already setting PS1 it will still be replaced. The only time I can imagine this not working is if they have a PROMPT_COMMAND that overwrites the PS1, or something similar.

Anything you do from the command line is likely to be overridden by your configuration files. You'll need to modify the appropriate file slightly to use something like

# *After* you make any changes to PS1
if [[ -n $MY_PS1 ]]; then
    PS1=$MY_PS1
fi

If you invoke the shell as

MY_PS1='yes master? > ' bash   # or zsh

then MY_PS1 will be used instead of whatever is configured in .{bash,zsh}rc .

I found the answer. We should just create a dir with custom config files, like .zshrc :

source $ZDOTDIR_ORIG/.zshrc
export PS1="[x] "$PS1

and then use a script to execute child shell remembering original value of ZDOTDIR var under ZDOTDIR_ORIG name, like this pseudopythoncode:

if os.environ.has_key('ZDOTDIR'):
    zdotdir = os.environ['ZDOTDIR']
else:
    zdotdir = os.path.expanduser('~')

os.environ["ZDOTDIR"] = shellConfPath
os.environ["ZDOTDIR_ORIG"] = zdotdir

And then execute the shell. It will use the config file from ZDOTDIR directory.

wouldn't export PS1='yes master? >' zsh/bash export PS1='yes master? >' zsh/bash work?

I see no reason why it shouldn't be as simple as that and yet achieve what you were after

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM