2

I usually zsh. I want to execute a function in bash and return back to my original shell. I can do this manually, but I want to automate it. How can I do that?

For example, I am on zsh. I want to execute func, which is a function() in bigscript.sh file. I am sourcing the bigscript.sh file in my .zshrc I tried adding shebang #!/bin/bash in the bigscript.sh file, but that does not change the shell. I tried another approach of putting function in a standalone function.sh file and added shebang to it. It worked. However, I do not want to break the files into smaller .sh files.

So, any suggestions on changing the shell for a single function..?

2
  • @KamilMaciorowski sorry, typo..
    – ARK
    Commented Feb 22, 2018 at 20:04
  • You should be able to schedule bash to take input from a "here-document". I haven't used this in zsh, but the facility seems to be much the same as in bash.
    – AFH
    Commented Feb 22, 2018 at 20:21

1 Answer 1

2

Generally if you want to execute a bash function, you'll need to use the bash interpreter. Some functions may work in zsh as it has a similar syntax, but there are differences that may be tripping you up. Try this.

bash -c 'source bigscript.sh && func'

There are a few gotcha's with doing this. If the function is setting variables or aliases, those will not be visible to zsh. Also, if bigscript.sh contains any code that isn't inside a function, it will be executed when the sourcing is done, so be careful. The last thing you want is for bigscript.sh to accidentally delete some files or make any other unexpected changes. If bigscript.sh is just a collection of functions, you should be fine. Your zsh session will be able to collect the function's return/exit code and any output it writes to stdout.

1
  • Actually its the other way around. bigscript.sh was written for bash. I am using zsh-5.4 for my daily development. This zsh version still has the bug for square brackets. So I end up using bash for executing functions in that script.
    – ARK
    Commented Feb 22, 2018 at 20:37

You must log in to answer this question.

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