I have the following alias on my local machine:

alias gom='mosh -- user@host  "/path/to/specific/zsh"'

I use it to connect to a remote machine with an interactive shell. I would like to modify this alias to run a specific function my_func defined in .zshenv in the remote machine as soon as I log in on it (when using this alias).

How can I do so?

I have tried the following:

  • alias gom='mosh -- user@host "/path/to/specific/zsh"; my_func;'
  • alias gom='mosh -- user@host "/path/to/specific/zsh; my_func"'
  • alias gom='mosh -- user@host "/path/to/specific/zsh & my_func"'

Some of the above solutions disconnect me from the remote sessions, while others don't, but none of them work.

  • Why don't call the function inside .zshrc? – cuonglm Aug 31 '15 at 18:12
  • Thanks @cuonglm. It's not an option for reasons that I can't easily explain. I need to do it when, and only when I connect using this alias. – Amelio Vazquez-Reina Aug 31 '15 at 18:15
  • I think you can't. /path/to/specific/zsh spawn a interactive shell, then you must call my_func explicitly. For non-interactive shell, you can mosh -- user@host /path/to/specific/zsh -c "my_func"' – cuonglm Aug 31 '15 at 18:18
% exec zsh
% MAGIC=1 exec zsh
hello world
% grep -1 hw .zshrc

function hw { print "hello world" }
[[ -n $MAGIC ]] && hw


So uh remotely

% ssh localhost "MAGIC=1 zsh -i"

or something

| improve this answer | |

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.