-1

I have a rather long bash script for setting up a server cluster. I'd like to start switching over to fish interpreter but I'd like to do it in steps. Can I keep my bash script as is and add the fish command to switch to fish prompt and the rest of the bash script will execute the rest of the commands from the fish interpreter, even though it's a #!/bin/bash script?

xendi
  • 374
  • 5
  • 8
  • 21

1 Answers1

2

If you're thinking you might be able to do this:

#!/bin/bash
echo this part is bash commands
for num in {1..10}; do echo $(( num*num )); done
fish
echo this part is fish commands
for num in (seq 10); math $num \* $num; end

then no, it doesn't work like that. That would start an interactive fish session in the middle of the script, and when you exit from that fish process, bash would attempt to execute the rest of the commands as bash commands. You'd clearly see syntax error messages.

You would have to do fish -c 'fish commands ...', but that can easily lead to quoting hell. The safest way to mix languages in a bash script is to use a quoted heredoc. You can expose bash variables for the fish part by using the environment.

#!/bin/bash
echo this part is bash commands
for num in {1..10}; do echo $(( num*num )); done
export foo=bar
fish <<'END_FISH'
    echo this part is fish commands
    for num in (seq 10); math $num \* $num; end
    echo the bash variable foo has value $foo
END_FISH
glenn jackman
  • 4,320
  • 16
  • 19