Can I run Bash scripts in FreeBSD without modifying them?
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 fordash
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
ormksh
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 likedash
or NetBSDsh
. On OpenBSD, it's based onpdksh
, on many commercial Unices, it's based on ksh88 (which is the basis for the Unix/POSIXsh
specification). Someksh88
-based shells andbash
are the only two shells that have been certified as being a Unix compliantsh
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.
Related videos on Youtube
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, 2022Comments
-
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 truesh
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 over 11 years"Don't want to install Bash" - OP doesn't want to run bash.
-
arved over 11 yearswell it could be zsh too, thats why i wrote "favorite shell"
-
user1115057 over 11 yearsBut, 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, thanbash filetoexecute.sh
orzsh filetoexecute.sh
. Also, Strange the way you say it... bash ./something... Never saw someone using the ./ as an argument -
arved over 11 yearsAnother option would be to modify your system to install zsh as /bin/sh.
-
Adaephon about 9 yearsWhile 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 over 6 yearsNote 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 over 6 yearsA symlink would be better. Otherwise, /bin/bash would not automatically be updated when /usr/local/bin/bash is updated.
-
Stéphane Chazelas over 6 yearsNote 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 thanbash
. It's also generally faster thanbash
and has some optimisations that some otherwise leaner/faster shells likedash
don't have (like optimizing out some forks). On FreeBSD though, changing sh to bash or zsh would indeed affect performance. -
Philippos over 6 years@StéphaneChazelas Thank you for you edit and for this clarification. That's what I meant: Not
zsh
instead ofbash
, 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 installingbash
would not help.