Reputation: 26467
I have a bash file that needs to get sourced. Users might have a csh without knowing (default configuration) so I wanted to change the shell to bash but sourced the file as that was the user's intention.
There are a lot of help around this and the resulting code would be:
#!/bin/csh (AND bash!)
[ "$?shell" = "1" ] && bash --login -c 'source this_file; bash' && exit
...
Everything works as expected besides the fact that the sourced file this_file
must be hard-coded. In csh $_
would contain source this_file
as that was the command that started sourcing the script, but there is no way I can pass it to the bash command.
This means:
If I use:
... && set this_file=($_) && bash -c "$this_file; bash" && ...
bash will complain that the parenthesis are wrong (this happens the second time as bash is started and tries to source this_file
If I use:
... && bash -c ""$_"; bash" && ...
bash gets a broken command that doesn't work either: bash -c "source" "this_file" "; bash"
If I use:
... && bash --login -c "$_; bash" && ...
csh gets a broken command: `Unmatched ".
I can't find out how to use $_
with an accepted bash syntax that passes the value as a single command (i.e. bash -c "source this_file; bash"
)
This is the test:
cat >a.sh<<'EOF'
[ "$?shell" = "1" ] && bash --login -c 'source a.sh; bash' && exit
a=1
EOF
And then I expect this to work:
$ csh -c 'source a.csh'
$ echo $a
1
I'm pretty sure it used to work... I'm trying to find out why it doesn't now. (I solved this problem using the tcl modules package, but I'll give this a try)
Upvotes: 1
Views: 701
Reputation: 11
It is possible to pass arguments to a bash -c 'cmd'
construct, i. e. bash -c 'echo $@' arg0 1 2 3 4 5
!
#!/bin/csh (AND bash!)
#[ "$?shell" = "1" ] && bash --login -c 'source this_file; bash' && exit
[ "$?shell" = "1" ] && bash --login -c '${@}; bash' arg0 $_ && exit
Upvotes: 1