I read an interesting post this morning from Matt Harrison. He was trying to use pushd and popd in a shell script and they weren't working. This seemed odd to me: pushd and popd are both shell built-ins in bash.
The answer that he found was interesting. His distro of choice seems to be Ubuntu, which uses something called "dash" as its default (system) shell instead of bash. In fact, they even set up /bin/sh as a symlink to /bin/dash, whereas most distros have that symlink pointing to /bin/bash. I don't think I ever noticed dash before because when I set up a system, I always make sure to set bash as my default login shell.
I run shell scripts all the time, and I use pushd and popd on a regular basis. I've never had a problem. That's because I'm running bash, right? Well, that's part of it. But there's something else that I do that in my small experience seems to be abnormal in the shell scripting world. The tops of all of my scripts start with this line:
#!/bin/bash
The vast majority of shell scripts that I see lately start with this line:
#!/bin/sh
If I used /bin/sh like everybody else seems to, then I would have discovered Matt's pushd/popd issue a lot sooner. As it turns out, dash doesn't have pushd and popd as built-ins. And from a quick glance on my system, they're not included on the default Ubuntu install. But since I specify /bin/bash, which has those commands built in, I've never had an issue.
Why do I specify /bin/bash in the first place? Common practice is to use /bin/sh, which I suspect is more for historical reasons than anything else. /bin/sh actually refers to the Bourne shell, as opposed to bash, which is the Bourne Again Shell (get it? it's funny!). From everything that I've heard, the original Bourne shell has never shipped with Linux.
Since I know I'm using bash and not Bourne, I specify bash. I'm anal. I'm obsessive compulsive. If being a geek didn't do that to me, going to cooking school finished the job. I've had students ask me why I type vim instead of vi when editing a file. The simple answer is, because I'm using vim, not vi. While most distros set up an alias to run vim when typing in vi, not every distro does so. I don't want any surprises.
Shortcuts are nice, but are you doing something because it's easier, because it's faster, because "that's how it's always been done", or because you've consciously decided that it's the most effective way to do it? It's something to think about.
Yes I need to come into the 2000's and realize that most machines I'll be using will have bash installed (even if they aren't the default), and use bin/bash for my hash bang.....
ReplyDelete