2

I want to create a new instance of tcsh and source an arbitrary script, all in one step. The problem is that when I use the -c option, the shell instance closes as soon as the script is complete. So, in the following trivial example the pushd command completes successfully but then the shell exits:

tcsh -c "pushd ~/some/directory/of/interest"

How can I source a script which modifies the environment and then work interactively within that environment? This is most useful when used in conjunction with programs like ssh or screen, as in the following:

ssh -t user@host 'tcsh -c "source ~/test_environment.csh"'

2 Answers 2

4
+150

Our solution will involve two steps.

  1. Pass Environment variable containing path to script you want to source to new tcsh instance.
  2. Have tcsh source the script pointed at by this environment variable

For step 1 ssh will run the command you specify in your default shell (the one in the destination server's /etc/passwd) so I'll give you several solutions for this.

  • If destination shell is sh/bash: Connect to ssh server with command: ssh -t user@host 'export SOURCESCRIPT=/tmp/tmp.sh; exec /usr/bin/tcsh'
  • If destination shell is csh/tcsh: Connect to ssh server with command: ssh -t user@host 'setenv SOURCESCRIPT /tmp/tmp.sh; exec /usr/bin/tcsh'
  • If you can modify the destination's ssh server config, add/modify AcceptEnv option in /etc/ssh/sshd_config to allow SOURCESCRIPT environment variable to be passed (e.g. AcceptEnv SOURCESCRIPT), set SOURCESCRIPT in local environment and connect with command: ssh -t -o SendEnv=SOURCESCRIPT user@host 'exec /usr/bin/tcsh'

For step 2, we modify ~/.tcshrc to add the following:

if $?SOURCESCRIPT then
    source $SOURCESCRIPT
endif
2
  • This is a very nice, straight-forward workaround! It requires modifying the user's ~/.tcshrc beforehand, but that is not a problem in my scenario.
    – nispio
    Aug 26, 2015 at 19:37
  • Glad to hear it was successful! Thanks for catching the error, fixed that equal sign. Aug 26, 2015 at 21:07
-1

Correct me if I'm wrong, but can't you use the '-m' option for tcsh to load a startup script? I just read the man page and thought that you might be able to make the '~/.tcshrc' file a symlink to another script. In case there's something screwey going on with the commands, maybe you could try the '-e' or '-m' options.

Hope this helps!

EDIT: Or maybe you could use the '-i' option for interactive in conjunction with the '-c' option :)

1
  • ~/.tcshrc is my startup script, so I can't just replace it with a symlink. I am looking for a way to optionally run an alternate startup script.
    – nispio
    Aug 25, 2015 at 18:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .