8

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...

2
  • 1
    See stackoverflow.com/questions/5725296/…
    – jasonwryan
    Commented Aug 5, 2012 at 9:07
  • Saying "I want to run a bash script without installing bash" is equivalent of saying "I want to run a Python script without installing Python". If it's a bash script, it should be executed using a bash interpreter.
    – Kusalananda
    Commented Aug 17, 2017 at 8:43

4 Answers 4

7

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

bash ./script.sh
6
  • 1
    "Don't want to install Bash" - OP doesn't want to run bash.
    – Mat
    Commented Aug 5, 2012 at 13:01
  • 1
    well it could be zsh too, thats why i wrote "favorite shell"
    – arved
    Commented Aug 5, 2012 at 13:48
  • 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 Commented Aug 5, 2012 at 15:36
  • Another option would be to modify your system to install zsh as /bin/sh.
    – arved
    Commented Aug 5, 2012 at 16:19
  • @arved Of course, after you've made sure that this change does not break any system scripts.
    – Kusalananda
    Commented Aug 17, 2017 at 8:54
4

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

#!/usr/bin/env bash
1
  • 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.
    – Adaephon
    Commented Jan 30, 2015 at 10:53
2

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.

2
  • 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. Commented Aug 17, 2017 at 10:20
  • @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.
    – Philippos
    Commented Aug 17, 2017 at 10:25
0

If you really want to run Linux based written script using /bin/bash in FreeBSD without modifying them, then you can simply copy /usr/local/bin/bash to /bin/bash at FreeBSD and it will work.

1
  • 1
    A symlink would be better. Otherwise, /bin/bash would not automatically be updated when /usr/local/bin/bash is updated. Commented Aug 17, 2017 at 10:01

You must log in to answer this question.

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