Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've never worked outside bash. I have a lot of simple bash scripts. (Usually just stringing together commands with the occasional pipe or output to file)

Will this break all those or are bash scripts and zsh scripts basically interchangeable?



As the other person said, if your scripts are executables with an sh or bash 'shebang' at the top, they will continue to use the same shell they always did.

But if you did want to run them with zsh, most simple scripts that just set environment variables, string together commands, or redirect to files should work without any changes. The most obvious difference for basic scripts like that is that zsh doesn't split parameter-expansions on white space by default (so if you did something like `opts='-a -b -c'; mycmd $opts` it wouldn't work without additional, minor, modifications)


The minor modification would be from `$opts` to `$=opts`.

I'd been using ZSH for a year as if it was Bash with better history and much better completions. The difference mentioned above was the only one I knew about. I kept writing my scripts in Bash and was quite happy with the UX.


>As the other person said, if your scripts are executables with an sh or bash 'shebang' at the top, they will continue to use the same shell they always did.

Cool! I do use shebangs, but my concern is that moving forward bash may not be ported to macOS. (Or will I probably be able to, worst case, compile it myself?)


If they start with a shebang like `#!/usr/bin/env bash` indicating which shell they are written in, it should be fine.


As all shell scripts should be.


Cool. I have a couple that I forgot to because they're extremely minor (basically just a 4 line set of commands into one) but I can append that easily.

Maybe I'll write a bash script to prep my poorly written bash scripts :D


I'd recommend to always use just POSIX shell syntax, or at least be aware when you're using bashisms. Bash additions don't buy you much, yet make your script unportable. And using bashisms will get you actually in trouble in practice, since Debian uses dash as default shell, and now Mac OS is using zsh. Considering that shell syntax development has practically ended in 1993, relying on bashisms just isn't worth it.


To be honest I feel like this is throwing the baby out with the bathwater somewhat. If you're writing a script for a system without bash you'll almost certainly know it, so you might as well just put bash in your shebang. I spent a couple of years using #!/bin/sh and avoiding bashisms, but it was literally never useful so I just went back to using #!/usr/bin/env bash.

Of course it goes without saying that you shouldn't use #!/bin/sh then use bash...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: