Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Most code I write is in PHP. I have recently started learning shell scripting. Most of the resources and tutorials that I've come across are specific to Bash. Some warn about bashisms and some don't. I've been reading a lot on here and Stack Overflow.

Whenever an answer uses bashisms, someone will inevitably comment to say:

You shouldn't use <insert bashism here>. It's not portable.

This happens even when the question was tagged with bash. To me, that's like telling a PHP programmer that they shouldn't use code that's new in PHP 5 because it can't be used with PHP 4. Or telling someone they shouldn't write something for Mac because it can't be used on Windows.

When I write in PHP, I pick a minimum requirement and I write forward-compatible code. I don't worry about making it backward-compatible.

If I use #!/bin/bash as the shebang, why shouldn't I use bashisms? I'm starting to get the impression that some people just like to bash bashisms (pun intended) just for the sake of it.

People often use bash and shell interchangeably - probably due to the fact that bash is the default shell on many systems. So I can understand adding a comment to warn that the code uses bashisms, but I don't understand the implication that it's wrong to use them.

Obviously, if I am writing a script strictly for personal use I can write it in whatever language I want. But I'd like to think some of the code I write could be useful to others.

I tried searching for an answer to my question before posting. I found a lot of information about how to test for portability, but couldn't find anything about when it's important to do so.

So, when is it important to write portable scripts?

For example,

  • what types of scripts should be as portable as possible?
  • how common are systems that do not have Bash installed?
  • if the system has Bash installed, will it also have the GNU version of find and other utilities?
share|improve this question
2  
I upvoted this question and answered it, but the more I think about it only the last 2 questions fit with the site. The others a "primarily opinion based". –  jordanm Mar 14 at 6:12
    
There is also a meta aspect in this question: While it may not help the initial answering of the question, comments like "this is not portable" can be very helpful for other readers stumbling accross the question/answer months or years later. –  Bananguin Mar 14 at 10:02
    
@Bananguin Comments saying "this is not portable" don't bother me. That's just an FYI. I've made those types of comments myself. It's the "You shouldn't use..." that bothers me. It implies that there is something wrong with the answer. That would be valid if the answer was recommending deprecated code, for example. So it had me wondering what is wrong with Bash. –  toxalot Mar 14 at 10:52
    
+1. Especially when the question has bash tag alone, someone comments "It's bashism and not portable". When I answer a question that's tagged C, I don't care whether it's portable to Java or not. –  Blue Moon Jul 23 at 14:43

1 Answer 1

up vote 8 down vote accepted

As a member of this community I do not often see people disregarding bashisms for things that target bash. When talking about portability, GNUisms are much worse than bashisms. As long as you use bash for your shebang, you can expect the script to be executed using bash. However, you can't guarantee the version unless you explicitly check. Bash version 4 brought some useful features such as associative arrays, but Bash v3 is still widely used on OS X and RHEL 5.

what types of scripts should be as portable as possible?

This is more about your needs and what you choose to support as the developer. If you are writing an install script for an application that supports all types of *NIX, then portability will be very important.

how common are systems that do not have Bash installed?

FreeBSD and Solaris 10 are examples of systems that do not come with bash installed by default. Most Linux systems will have it, with the exception of embedded systems.

if the system has Bash installed, will it also have the GNU version of find and other utilities?

No, and this is the real issue with portability. If portability is important, you should only use shell command features that are defined by the POSIX standard. GNU utilities can be installed on non-GNU systems such as FreeBSD, but they are unlikely to be first in PATH, and you can not rely on those tools to be installed.

share|improve this answer
    
I think it's more on SO than here where I often see people disregarding bashisms. I just know that I've noticed it often enough to frustrate me and prompt me to ask this question. –  toxalot Mar 14 at 6:20
    
So if GNU utilities are installed but not first in PATH, is there a way to call them specifically? Otherwise, why have them installed at all? –  toxalot Mar 14 at 6:22
    
@toxalot: Binaries and scripts are called explicitly by using absolute paths. –  Bananguin Mar 14 at 10:05
    
@Bananguin So if someone had Bash and GNU utilities installed (but not in PATH), they could use the script if they edit it to use absolute paths for find and whatever other GNU-specific utilities I had used? Obviously, that would not be user-friendly for an installer script. But I'm thinking of just putting something on GitHub for others to grab if they so desire. I don't care that it's not portable to every system. I just don't want it to be useless on many systems. –  toxalot Mar 14 at 11:15
    
@toxalot: yes that could work. if you put something like FIND=/opt/gnu/dispicable/bin/find at the beginning of your script and then use ${FIND} instead of find in your script you give people one (!) place to put their install paths and make your script use the correct tools. –  Bananguin Mar 14 at 12:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.