while with ZDOTDIR, you can tell zsh
to interpret a file called .zshrc
in any directory of your choosing, having it interpret any file of your choosing (not necessarily called .zshrc
) proves quite difficult.
In sh
or ksh
emulation, zsh
evaluates $ENV
; so you could add emulate zsh
at the top of your /path/to/file
and do:
ssh -t host 'zsh -c "ARGV0=sh ENV=/path/to/file exec zsh"'
Another very convoluted approach could be:
ssh -t host 'PS1='\''${${functions[zsh_directory_name]::="
set +o promptsubst
unset -f zsh_directory_name
unset PS1
. /path/to/file
"}+}${(D):-}${PS1=%m%# }'\' exec zsh -o promptsubst -f
That one deserves a bit of an explanation.
${foo::=value}
is a variable expansion that actually sets $foo
. $functions
is a special associative array that maps function names to their definitions.
With the promptsubst
option, variables in $PS1
are expanded. So, upon the first prompt, the variables in that PS1 will be expanded.
The zsh_directory_name
function is a special function that helps expanding the ~foo
to /path/to/something
and the reverse. That's used for instance with %~
in the prompt so that if the current directory is /opt/myproj/proj/x
you can display it as ~proj:x
by having zsh_directory_name
do the mapping proj:x
<=> /opt/myproj/proj/x
. That's also used by the D
parameter expansion flag. So if one expands ${(D)somevar}
, that zsh_directory_name
function will be called.
Here, we're using ${(D):-}
, ${:-}
, that is ${no_var:-nothing}
expands to nothing
if $no_var
is empty, so ${(D):-}
expands to nothing while calling zsh_directory_name
. zsh_directory_name
has previously been defined as:
zsh_directory_name() {
set +o promptsubst
unset -f zsh_directory_name
unset PS1; . /path/to/file
}
That is, upon the first PS1 expansion (upon the first prompt), ${(D):-}
will cause the promptsubst
option to be unset (to cancel the -o promptsubst
), zsh_directory_name()
to be undefined (as we want to run it only once) $PS1
to be unset, and /path/to/file
to be sourced.
${PS1=%m%# }
expands (and assigns $PS1
) to %m%#
unless PS1 was already defined (for instance by /path/to/file
after the unset
), and %m%#
happens to be the default value of PS1
.
code as such
! – hjkatz May 23 '14 at 20:19