Can I run Bash scripts in FreeBSD without modifying them?

18,402

Solution 1

You can call your favorite shell with the script as a parameter.

bash ./script.sh

Solution 2

I run FreeBSD myself. I found that starting the script with the following greatly improves cross OS compatibility:

#!/usr/bin/env bash

Solution 3

You asked to correct you when you were wrong. I'll try:

  • Linux scripts are written in bash: Well, some are written for bash, others for dash and hopefully a lot potable for any POSIX-compliant shell.
  • Bash script usually #!/bin/sh If they rely on non-standard bash features, it is highly recommended to have a #!/bin/bash shebang
  • In GNU/Linux, /bin/sh is Bash That depends on the distribution. It can also be dash or mksh on Debian for instance.
  • In FreeBSD, /bin/sh is not bash, it's the true sh. There is a lot of sh implementations. Which one is "true"? On FreeBSD, sh is based on the Almquist shell like dash or NetBSD sh. On OpenBSD, it's based on pdksh, on many commercial Unices, it's based on ksh88 (which is the basis for the Unix/POSIX sh specification). Some ksh88-based shells and bash are the only two shells that have been certified as being a Unix compliant sh implementation (when built with the right flags and in when in the right environment).

If you have scripts with /bin/sh shebang and want them to be executed by zsh you can try passing the script to zsh, but this will fail as soon as this script calls another script (and you don't want to modify it).

So I only see the possibility to have a symbolic link from /bin/sh to your zsh. But I don't recommend to do this, as it may have drastic impact on your boot time and will not even help you for some of your linux scripts.

Share:
18,402

Related videos on Youtube

user1115057
Author by

user1115057

Im interested in learning how to do low level stuff like kernel, os, driver.... Thing i need to learn: Programming (i already know how to write in C, C++, but i still need to think like a programmer...).

Updated on September 18, 2022

Comments

  • user1115057
    user1115057 over 1 year

    Correct me if I'm wrong:

    • "sh" script != "bash" script
    • Linux script are written in Bash
    • Bash script usually #!/bin/sh
    • In GNU/Linux, /bin/sh is Bash
    • In FreeBSD, /bin/sh is not bash, it's the true sh

    So if I want to use a Linux script in FreeBSD, and I run ./script.sh in the shell, it will run the Bash script in "sh" and not Bash, since /bin/sh in FreeBSD is not Bash.

    Is there a way I could run those Bash scripts, without modifying it? So no modification to the #!/bin/sh statement in the script file to point somewhere else?

    I would like to run Bash script trough Zsh, if possible. Don't want to install Bash, and since Zsh can run Bash scripts...

  • Mat
    Mat over 11 years
    "Don't want to install Bash" - OP doesn't want to run bash.
  • arved
    arved over 11 years
    well it could be zsh too, thats why i wrote "favorite shell"
  • user1115057
    user1115057 over 11 years
    But, without calling the program bash (or zsh) with a script as an argument? In the last resort, I could do that, but the ./filetoexecute.sh is faster to write, than bash filetoexecute.sh or zsh filetoexecute.sh. Also, Strange the way you say it... bash ./something... Never saw someone using the ./ as an argument
  • arved
    arved over 11 years
    Another option would be to modify your system to install zsh as /bin/sh.
  • Adaephon
    Adaephon about 9 years
    While that is correct and helpful when writing a new script it does not really answer the question here. The OP asked explicitly for a solution where already existing files do not need to be edited before they can be used.
  • Philippos
    Philippos over 6 years
    Note that while this solution will execute script.sh with the selected shell, any script called from within that script, will still default to the shebang given in the script.
  • Stéphane Chazelas
    Stéphane Chazelas over 6 years
    A symlink would be better. Otherwise, /bin/bash would not automatically be updated when /usr/local/bin/bash is updated.
  • Stéphane Chazelas
    Stéphane Chazelas over 6 years
    Note that while I wouldn't recommend installing zsh as /bin/sh either, it may not impact boot time that drastically. Thanks to its modular design, it's leaner than bash. It's also generally faster than bash and has some optimisations that some otherwise leaner/faster shells like dash don't have (like optimizing out some forks). On FreeBSD though, changing sh to bash or zsh would indeed affect performance.
  • Philippos
    Philippos over 6 years
    @StéphaneChazelas Thank you for you edit and for this clarification. That's what I meant: Not zsh instead of bash, but instead of a fast scripting shell. Another problem not yet addressed here is that scripts for linux and not tested anywhere else may use GNU extensions on standard tools., so even installing bash would not help.