Ashot
Ashot

Reputation: 10959

Entering cshell from bash

I have a bash script and need to run some commands in cshell inside it.

#!/bin/bash

echo entering_to_cshell
csh
echo in_cshell
exit
echo exited_from_cshell

Why doesn't this script run as expected? It only prints entering_to_cshell and it doesn't exit from cshell.

Upvotes: 3

Views: 2008

Answers (2)

m3adow
m3adow

Reputation: 50

By using

csh

you start a new subshell where your script isn't executed. That's why none of your following commands are executed. Your script waits for this subshell to end which, as you noted, never happens.

Try

csh -c "echo in_cshell"

This way you don't create a new subshell which isn't impacted by your script.

Upvotes: 2

Shawn Chin
Shawn Chin

Reputation: 86854

By simply calling csh in your script, you're starting an interactive csh subshell. You'll notice that once you quit from the csh session, your script will then continue to with the subsequent echo and quiting on exit.

To pass a series of commands to csh from you bash script, one approach would be to use the Here Document syntax to redirect commands to csh.

#!/bin/bash

echo entering_to_cshell

csh <<EOF
echo in_cshell
exit
EOF

echo exited_from_cshell

The lines between the EOF entries will be treated as a script that is interpreted by csh.

Upvotes: 0

Related Questions