How to execute Zsh shell commands in Bash Script

11,249

You can use it like that :

#!/bin/bash

/bin/zsh -i -c hello

-i : Force shell to be interactive

Then, if the shell is interactive, commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc (this is usually your $HOME/.zshrc)

-c : Run a command in this shell

Share:
11,249
abhiomkar
Author by

abhiomkar

"There are no accidents!"

Updated on June 14, 2022

Comments

  • abhiomkar
    abhiomkar almost 2 years

    I want to execute Zsh function command in Bash script. Here is an example:

    ~/.zshrc

    hello () {
      echo "Hello!"
    }
    

    hello.sh

    #!/bin/bash
    hello
    

    executing above bash script in zsh

    (zsh) $ ./hello.sh
    hello command not found
    

    I also tried with heredocs:

    #!/bin/bash
    /bin/zsh - <<'EOF'
      hello
    EOF
    

    executing above script with heredocs also says command not found error.

    Any suggestions?

    Thanks!