0

I've got a couple flags in my zsh configuration - and I've been trying to figure out a way to startup a new shell with them set, but haven't found a solution yet. Any suggestions? For example, I'm looking to do something like:

zsh --login --export=RC_BOOTSTRAP:true,RC_DEBUG:true

Edit 01

Essentially, I'm trying to setup custom shell options. The best method I've come up with so far is:

# set the flags as environment variables and restart the shell
exec env RC_BOOTSTRAP=true RC_DEBUG=true zsh --login

And in my startup files, I have:

# I don't want these flags to propagate into additional shells
# so they need to be 'unexported'
RC_TMP_BOOTSTRAP=${RC_BOOTSTRAP:-false} && unset RC_BOOTSTRAP
RC_BOOTSTRAP=${RC_TMP_BOOTSTRAP} && unset RC_TMP_BOOTSTRAP
RC_TMP_DEBUG=${RC_DEBUG:-false} && unset RC_DEBUG
RC_DEBUG=${RC_TMP_DEBUG} && unset RC_TMP_DEBUG

This works, but it's pretty ugly. Any suggestions?

Edit 02
Based on the solution by @gilbert:

exec env RC_FLAGS='RC_BOOTSTRAP=true RC_LOG_LEVEL=4' zsh --login
[[ -n $RC_FLAGS ]] && { eval "$RC_FLAGS"; unset RC_FLAGS; }
nfarrar
  • 2,291
  • 4
  • 25
  • 33
  • Do you have a .zshrc file? – kometen Oct 24 '15 at 14:27
  • Of course. My rcfiles do some things based on flags. Currently, I've got a local configuration, where I set the flag values (bootstrap, debug, etc). I'm looking for a more convenient way to restart the current shell (`exec zsh --login`) and set the flag at the same time, rather than having to edit the flag in the configuration file and then restart the shell. – nfarrar Oct 24 '15 at 14:51

2 Answers2

1

Your temporary variables aren't necessary:

: ${RC_BOOTSTRAP:=false}
: ${RC_DEBUG:=false}
# Unexport them
declare +x RC_BOOTSTRAP RC_DEBUG

The first two lines, by the way, are not unique to zsh; they would work in any POSIX-compatible shell. Each variable, if not already present in the environment, is set to false.

Starting the shell with these variables doesn't necessarily require env:

RC_BOOTSTRAP=true RC_DEBUG=true exec zsh --login
chepner
  • 497,756
  • 71
  • 530
  • 681
0

A bit uglier in syntax, but fewer lines:

exec env eval_preset='RC_BOOTSTRAP=true;RC_DEBUG=true' zsh --login

And in the startup file:

eval "${eval_preset:-true}"; unset eval_preset

Beware, I haven't actually run these so I leave debugging to the student.

Gilbert
  • 3,740
  • 17
  • 19