nfarrar
nfarrar

Reputation: 2381

Start Zsh with 'Custom Options'

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; }

Upvotes: 0

Views: 812

Answers (2)

chepner
chepner

Reputation: 532093

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

Upvotes: 1

Gilbert
Gilbert

Reputation: 3776

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.

Upvotes: 0

Related Questions