Sunday, 20 December 2020

Today I learned ... when is Bash not Bash ?

 Having written Bash ( Bourne Again Shell ) scripts for the longest time, I couldn't quite work out why some things that worked on my Mac did NOT work on my colleague's Mac, even though he was using my script .....

TL;DR; the major difference was the shell that each of us was using.

For me, it's Bash all the way ...

echo $SHELL

/bin/bash

whereas, for him, that same command return: -

/bin/zsh

Now I typically write my scripts the same way, with line 1 reading: -

#!/bin/bash

which works for me ....

However, for him, he was essentially trying to run a Bash script in ZSH, but using the older default version of Bash.

When he ran /bin/bash to switch to Bash rather than ZSH, all was well ....

Thankfully, the internet came to our rescue: -


which says, in part: -

If your scripts start with the line #!/bin/bash they will still be run using bash, even if your default shell is zsh.

I've found the syntax of zsh really close to the one of bash, and I did not pay attention if there was really some incompatibilities. I switched 6 years ago from bash to zsh seamlessly.

and, even more importantly: -

Hardg-coding the path to the shell is bad advice, even if it's done frequently. You should use #!/usr/bin/env bash instead, especially on macOS, where the default bash is severely outdated and new versions are virtually always installed into a different path.

Following that second piece of advice, I changed my script's first line to read: -

#!/usr/bin/env bash

and, quelle surprise, it just worked for my colleague, from within a ZSH session.

As I say, TIL !

No comments:

Today I Learned - more about Git config

Whilst trying to create a container image from a project on GitHub, I hit an issue with the cloning process of the GH repository ... Specifi...