4 ms·
Why not sh instead of bash. This is a script; it is not an interactive shell. sh is faster as a scripting language. Don't take my word for it. If you are us
by 1vuio0pswjnm7 5y ago
Why not sh instead of bash. This is a script; it is not an interactive shell. sh is faster as a scripting language. Don't take my word for it. If you are using a UNIX-like OS such as BSD or GNU/Linux, sh has been chosen as the default scripting shell, not bash. Bash might be the default login shell (interactive use) but it is not the default scripting shell (non-interactive use).
This is only one function. We cannot see the rest of the script. Maybe there is a compelling reason1 to to use pushd and popd instead of using sh features.
1. i.e., no way to achieve same result in sh
- chmaynard 5y agoFYI, here is the entire script: https://raw.githubusercontent.com/chmaynard/Sources/core/workflow.sh https://raw.githubusercontent.com/chmaynard/Sources/core/wor...
- nerdponx 5y agoI would add a `#!/usr/bin/env bash` at the top, so readers know that it's meant for Bash.
- AlphaSite 5y agoIs comparability with sh really a worthwhile goal anymore? I haven’t seen many places with sh but not full bash (busybox being the main exemptions but that breaks most things due to muslc anyway).
- 1vuio0pswjnm7 5y agoWhat OS are you using. It may use bash for login, but I would be willing to bet it does not use bash for scripting (non-interactive use). It is possible that many shell script authors do not understand the difference between a login shell (optimised for interactive use) and a scripting shell (optimised for non-interactive use). Thus the comparison could be worthwhile. sh is faster, more portable and arguably presents fewer potential gotchas. FWIW, shellschock affected bash but not sh. NetBSD uses ash-derived sh as both login and scripting shell. No bash. Debian and many, many other GNU/Linux distributions (including musl-based Void) all use NetBSD-derived sh called "dash" as the default scripting shell. I am not aware of any Berkeley distribution that uses bash as the default scripting shell.
- nerdponx 5y agoSo use a shebang that specifies Bash, or invoke it with `bash` on the CLI. The default scripting shell might be important for typing like X11 startup scripts, but I don't see why you have to use it for standalone scripts just because it's the default.
- nerdponx 5y agoI write my scripts in Zsh and Python. Maybe once in a while Perl, if I really want Sed but can't figure out the right incantations with branching and the hold space. Why? Because they are programming languages that have features that I want. Bash has more features and functionality than Zsh. It's also pretty much ubiquitous. If your scripts are only meant to run on your computer, and you don't need them to work under Busybox or whatever, why constrain yourself to the limited set of features in POSIX sh?
- 1vuio0pswjnm7 5y ago"If your scripts are only meant to run on your computer, ..." is it likely you would be sharing them with others on the web. Thank you for sharing your story. Here's mine. I write scripts in sh. Why? Because it's small, fast and portable. On the OS I use it is not just "pretty much ubiquitous" it is ubiquitous. I have multiple computers running different OS and I want scripts that work on all of them. Some of these computers are resource-constrained and installing additional shells and other interpreters plus libraries is not worth the cost. Anyway, there is no need to do that. If I need scripts that are small, fast and portable, why should I use zsh plus perl or python.
- nerdponx 5y agoConsider that people don't write scripts to work with your system, but with their system. When I see a Ruby script, I don't complain to the author and tell them to use Python instead.
- 1vuio0pswjnm7 5y agoOK, now I understand how you interpreted my comment. Your interpetation is incorrect. I am neither complaining nor telling anyone what to use. I am asking a question. The comparison of Ruby versus Python is a little strange; they are two totally different languages. Bash OTOH is a superset of sh. Same language, with more features. I can usually modify most bash scripts I encounter to run under sh; most do not use the more advanced bash features. That is why I often wonder "Why not sh (for non-interactive use)?" You get better speed and portability and you do not have to install anything.