Moosy38 Moosy38 - 29 days ago 10
Bash Question

Setting up an Alias in tcsh for a function defined in a bash script

I have a cshrc file with aliases, in that file I want to set up an alias that point to a function defined in a bash file.

bash function file AliasFunc:

function aaa()
{

echo stackoverflow

}


Example .cshrc file:

bash -c 'source AliasFunc'

alias bashfunc aaa

bashfunc #calling bashfunc


sourcing the .cshrc file results in the following error:

bashfunc: Command not found.

Answer

What bash -c 'source AliasFunc' in your cshrc file does is start a new bash shell, load the AliasFunc file, and exit. It will not affect the parent csh shell at all.

It just so happens that bash and csh both have a source keyword, but that doesn't mean it's somehow "compatible". What you're trying to do now is equivalent of trying to import a Python module in Perl by starting a new Python process from within a Perl program.

What you can do is something along the lines of:

$ alias bashfunc 'bash -c "source AliasFunc && aaa"'
$ bashfunc
stackoverflow

Every time the bashfunc alias is invoked it will start a new bash shell which will source the AliasFunc file and will immediately run the bash aaa function.