This post is part 4 of 6 in the series Bash to Zsh

At Apple’s advice I’ve switched the login shell from Bash to Zsh on all my Macs. For the most part, what worked in Bash works in Zsh, but sometimes I do still want to get back to Bash to test something or to check something. You might imagine that simply typing bash from a Zsh prompt would get you a Bash shell, and you’d be right, sort of. When you just run the command bash you get a bare shell without the customisations that would have been applied when you opened a new Terminal window with Bash as your default shell. This will be immediately obvious because the prompt will be the basic bash-3.2$ as opposed to the hostname, current folder, and you’re username like you were used to.

The solution is really simple — pass the -l flag to signify that you want your new shell treated like a login shell, and hey presto, you’re back to Bash just like you remembered it 🙂

So, if you switch your Mac to Zsh, you get back to the Bash experience you had before with the following command:

bash -l

Tagged with:

This post is part 3 of 6 in the series Bash to Zsh

At Apple’s recommendation I’ve moved from Bash to Zsh on all my Macs. This has been a mostly smooth transition, but I have run into a handful of little gotchas. This week it was an error when trying to execute a command I’d been using in Bash for years. The error started zsh: no matches found.

The cause of this error is a subtle but important difference in how Bash and Zsh handle file globbing (expansion of the * character) when no files match the specified pattern. By default, Bash happily expands expressions that don’t match anything into an empty list. Zsh’s default behaviour is to raise an error and prevent the command executing.

TL;DR — setopt NULL_GLOB to enable Bash-like behaviour, and unsetopt NULL_GLOB to revert back to the Zsh-like behaviour.

Read more

Tagged with:

This post is part 2 of 6 in the series Bash to Zsh

Apple recently announced that it’s moving the MacOS from the Bourne-again Shell (Bash) to the Z Shell (Zsh), and advised developers to make the change now, so they’re ready when they remove Bash altogether in some later version of the OS. Since I’m a big believer in not swimming up-stream, I decided to take their advice and switched to the Z Shell immediately.

The first thing I noticed was that the default prompt Apple provides for Zsh on their OS gives a lot less information than their default for Bash did. This is a sample of their old Bash prompt:

bart-imac2018:Documents bart$

That tells me the machine I’m on (bart-imac2018), the folder I’m in (Documents), and the username the shell is running as (bart), and whether or not I have super-user privileges ($ means no, # means yes). These are all very useful things, particularly when you SSH around a lot and su/sudo to different accounts. Also, IMO showing only the top-level folder rather than the full path gives a nice balance between the prompt getting too big, and not knowing where you are. I’ve never felt an urge to change the Mac’s default Bash prompt.

I can’t say the same about the Mac’s default Z Shell prompt! This is what I get on the same machine with the Z shell:

bart-imac2018%

It only shows the machine name (bart-imac2018) and whether or not I have super-user privileges (% for no, # for yes)!

Thankfully getting back to the old Bash-like prompt is easy — the TL;DR version is that you simply need to add the following line to your ~/.zshrc file:

PROMPT='%m:%1~ %n%# '

If you’d like to understand how exactly that works, and what other choices you have, read on!

Read more

Tagged with:

Due to a problem with the File::HomeDir Perl module on recent versions of MacOS, the usual one-step instructions for installing Crypt::HSXKPasswd from CPAN don’t work on Macs ATM (June 2019).

TL;DR

sudo cpan -f -I File::HomeDir
sudo cpan Crypt::HSXKPasswd

Read more

Tagged with:

This post is part 79 of 92 in the series Programming by Stealth

Finally, after much teasing, we get our first taste of JavaScript Promises! This will just be a taste though, Promises are simultaneously really simple and really counter-intuitive. In many ways teaching promises reminds me a lot if teaching recursion — there is a tipping point where the concept goes from infuriatingly mind-bending to obvious and logical. Getting to that tipping point can be quite the challenge though.

So, we’re going to take it slow with promises. They will provide us with a way out of callback hell, but that path to salvation is unlikely to be obvious to you by the end of this instalment. It will take one or two more instalments until we get that far. All I can ask is that you please trust, me, how ever bumpy the journey gets, the destination is worth the struggle!

You can download this instalment’s ZIP file here.

Read more

Tagged with:

This post is part 78 of 92 in the series Programming by Stealth

For boring real-life reasons this instalment is a bit of an intermission. In the previous instalment we learned about so-called call-back hell, and were all set to learn how Javascript Promises would be our liberation, but that’s going to have to wait until next time. Promises are a very important concept, and I don’t want to rush them.

What we’re going to do in this instalment is focus entirely on my sample solution to the challenge set at the end of the previous instalment, which I’ve used as an opportunity to demonstrate two new tools to add to our programming tool belt — the micro-checking library is.js, and Bootstrap Popovers.

You can download this instalment’s ZIP file here.
Read more

Tagged with:

This post is part 77 of 92 in the series Programming by Stealth

My plans for this instalment were to quickly demonstrate so-called callback hell, and then move on to the solution, JavaScript Promises, but in light of some listener feedback I changed my plans a little. There was some confusion in the community about what callbacks really are, so, now seemed like an opportune moment to spend a little time re-familiarising ourselves with some callback basics. This sets things up for a bit of a teaser-ending because we’ll get as far as demonstrating callback hell, but not as far as using Promises to get back out of hell, that will have to wait until the following instalment!

You can download this instalment’s ZIP file here.

Read more

Tagged with:

This post is part 76 of 92 in the series Programming by Stealth

Having laid a very strong foundation in the previous instalment, we’re now ready to learn how to make HTTP requests with JavaScript using a technique known as AJAX.

We’ll start our journey into AJAX using more traditional JavaScript techniques, i.e. we’ll use callbacks to handle HTTP responses. As we’ll discover, this works very well for single AJAX requests, but the model really starts to get complicated when you have multiple inter-dependent requests. We won’t complicate things in this instalment though — we’ll start with just simple stand-alone requests this time.

You can download this instalment’s ZIP file here.

Read more

Tagged with:

This post is part 75 of 92 in the series Programming by Stealth

Having wrapped up our introduction to Mustache templates we’re going to spend the next few instalments learning about AJAX, a very powerful JavaScript technique for loading information from a given URL. In future instalments we’ll use this technique to load external templates and data for use with our Mustache templates.

AJAX is a mechanism for making a HTTP requests via JavaScript, so before we’re ready to learn about AJAX we need to take the time to learn about the HTTP protocol itself. Unless you understand the mechanics and the terminology of HTTP, AJAX-related documentation simply won’t make any sense.

Read more

Tagged with:

This post is part 74 of 92 in the series Programming by Stealth

In the previous instalment we got our first look at Mustache templates. In this instalment we’ll finish our look at this handy little third party library with a look at some of Mustache’s more advanced features. This will set us up perfectly to finally introduce AJAX into this series. This is an extremely common technique for fetching external resources with JavaScript. We’ll learn how to use AJAX to fetch both Mustache template strings and JSON data from URLs.

You can download this instalment’s ZIP file here.

Read more

Tagged with:

« go backkeep looking »