1

I need to source a tcsh script from my bash script. I have tried to do it like this:

tcsh -c "source ~/test.tcsh"

The problem with this command is that $_ is empty inside of the sourced tcsh script. Any ways to deal with that?

Thanks in advance!

2
  • This is similar to trying to parse a ruby script with python. The potential pitfalls are numerous. Commented Sep 25, 2014 at 17:06
  • Why is that? Wouldn't it be the tcsh that is sourcing a tcsh script? Commented Sep 25, 2014 at 17:13

1 Answer 1

1

The source command runs the script in the current shell. tcsh -c "source ~/test.tcsh" sources the file test.tcsh in the shell running the source command, which is an instance of tcsh, not the bash shell that runs tcsh. You aren't going to be able to execute a tcsh script in any way that affects the environment of the current bash shell. You could start a new bash shell that inherits from an instance of tcsh which was so modified, with something like

tcsh -c 'source ~/test.tcsh; bash'

but that may not be what you need. For instance,

tcsh -c 'source ~/test.tcsh; bash'
echo "OK, we're back"

will leave you in an interactive bash shell, but you won't see "OK, we're back" until after you exit that shell, which will cause tcsh to exit and return control to the original.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I am aware of that. But there was nothing about influencing the current environment in my question. The main problem is: why is $_ empty in the sourced script and if there is any way to make it not empty.
Since source ./test.tcsh is the first command executed by the tcsh process, there is no previous command for $_ to be set to. Were you expecting it to be exported by bash?
By the way, source here is irrelevant; since you are starting a new tcsh process anyway, tcsh ~/test.tcsh does the same thing, unless you are running more code than you indicate in the question.
Unfortunately, it doesn't actually matter if I call any command before source, i.e. tcsh -c 'ps; source ~/test.tcsh', $_ is always empty... What kind of sorcery is this?
Don't tell me I have to spawn tcsh to achieve this :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.