Bully Bash Mac OS
Bully Bash Mac OS
- On your Terminal: Type cd / to go to your home folder. Type touch.bashprofile to create your new file. Edit.bashprofile with your code editor (or you can just type open -e.bashprofile to open it in TextEdit).
- A FINAL UDPATE: For anyone struggling with compiling from source, as of September 29th, Apple has officially released patches for Mac OS X 10.9.5, 10.8.5 as well as 10.7.5: OS X bash Update 1.0 – OS X Mavericks; OS X bash Update 1.0 – OS X Mountain Lion; OS X bash Update 1.0 - OS X Lion.
Mac bash shell free download. Git for Windows Git for Windows is the Windows port of Git, a fast, scalable, distributed revision control system wi. Bash has been the default shell since Mac OS X 10.3 Panther. This series has grown into a book: reworked and expanded with more detail and topics. Like my other books, I plan to update and add to it after release as well, keeping it relevant and useful. Note: bash is also POSIX compliant, but it has more features. When you script against sh you ensure you don’t use any of those additional features. However, as macOS administrators, we can rely on a larger ‘common’ set. Bash has been present on macOS since Mac OS X 10.0 (and earlier incarnations in NeXTSTEP).
Supposedly, macOS 10.15 Catalina is slated to replace the default /bin/bash shell with zsh, or /bin/zsh. Before we talk about the differences let’s just say that bash is still here and if your script is called as bash then it will still work just fine. To quickly see which you’re using (e.g. when testing a new release), use $0:
echo $0
Z Shell or zsh for short was written by Princeton University student Paul Falstad in 1990. Most shells are just extensions of the Bourne shell (including bash) and work similarly but there are minor differences here and there. Yes, Z Shell comes with a control-R reverse incremental search, but that’s not a good reason to make this kind of change. Z Shell is more modern (e.g. more customizable autocompletion, use Alt + . to put parameters from the previous command into your next command, slicker tabbed auto-complete), considered by some to be more secure (not considered as such by others), and, well, it doesn’t make you wonder what the next Jason Bourne movie will be about (he invented bash, right?!?!).
So let’s go through some of the newish things to get used to. These include (but are not limited to) the following:
File globbing: Let’s take something like using an * to list file contents. So files=* in bash:
somefiles=*
Then let’s read the contents of that $file variable:
The output would be as follows, a basic list of files:
About This Mac.app Archive Utility.app DVD Player.app Directory Utility.app Feedback Assistant.app Folder Actions Setup.app Network Utility.app RAID Utility.app Screen Sharing.app Storage Management.app System Image Utility.app Wireless Diagnostics.app
Now let’s do the same operation in zsh:
somefiles=*
And then we’ll echo it:
echo $somefiles
The output shows that the * was accepted literally:
*
So to get the same result I often wrap my globs in a () like the following (which includes two to trap for hidden directories):
The security benefit here is that you don’t accidentally include something you’re not supposed to while getting more options for dealing with expansion. You can do this the same old way by enabling globsubst.
Directory Aliases: The next big difference would be directory aliases. The alias command in zsh allows for expanded aliases anywhere in a line. To put this in context, let’s grep output to something with an alias:
alias -g GS=' grep something'
Then:
cat somefile GS
Next on my list would be environment scripts. These include:
- zlogin
zlogout - zprofile
- zshenv
- zshrc
Additionally, zsh does spelling correction, which I’m personally not used to.
Some scripty bits to look out for with zsh:
- Don’t set BASH_ENV (obviously), ENV, or SHELL the same
- exec changes (see http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html for more on how zsh does this)
- Native hashed data structure support in zsh using typeset
- No -x like in bash
- Remove any PROMPT_COMMAND entries
- Replace any calls to getopts with zparseopts
- SHELLOPTS isn’t run at startup
- Use zcalc for all the maths including floating point support not present in bash natively: autoload -Uz zcalc
- -norc doesn’t skip anything
- -rcfile calls
There’s a zmv extension that can be loaded using
autoload -U zmv
This is because zsh is also modular. So you can load modules that help, for example, do additional file manipulation commands (zsh/files), use posix regex (zsh/regex) or deal with sockets (zsh/net/socket). Or to view a good list of plugins, see https://github.com/unixorn/awesome-zsh-plugins. Oh there are bundles as well. You still awake?
If so, what do you lose? Honestly not a lot. Bash restricted mode, which I’ve only very rarely used:
bash --restricted
And no posix mode:
bash -o posix
Again, also not used much…
In general, when scripting automation for the Mac, I’d say obviously test all your scripts – especially if you aren’t specifically invoking them through /bin/bash. Many of these changes won’t be very impactful. Maybe you’ll have to get used to something working just a tiny bit differently when you’re interactively navigating through the shell; no big deal. But for scripting, definitely consider the globbing whatnot as something to look out for and know that if it breaks a script, rather than just calling bash to run, think about moving it over to the new default shell. Because it’s this way for a reason. And that reason probably isn’t just that some developer didn’t like the story or acting in the Bourne Legacy (although it might have been that bad)…
Apple has announced that in macOS 10.15 Catalina the default shell will be zsh
.
In this series, I will document my experiences moving bash
settings, configurations, and scripts over to zsh
.
- Part 1: Moving to zsh (this article)
- Part 2: Configuration Files
- Part 3: Shell Options
- Part 4: Aliases and Functions
- Part 5: Completions
- Part 6: Customizing the
zsh
Prompt - Part 7: Miscellanea
- Part 8: Scripting
zsh
zsh
(I believe it is pronounced zee-shell, though zish is fun to say) will succeed bash
as the default shell. bash
has been the default shell since Mac OS X 10.3 Panther.
This series has grown into a book: reworked and expanded with more detail and topics. Like my other books, I plan to update and add to it after release as well, keeping it relevant and useful. You can order it on the Apple Books Store now.
Why?
Mac Os Mojave
The bash
binary bundled with macOS has been stuck on version 3.2 for a long time now. bash
v4 was released in 2009 and bash
v5 in January 2019. The reason Apple has not switched to these newer versions is that they are licensed with GPL v3. bash
v3 is still GPL v2.
zsh
, on the other hand, has an ‘MIT-like’ license, which makes it much more palatable for Apple to include in the system by default. zsh
has been available as on macOS for a long time. The zsh
version on macOS 10.14 Mojave is fairly new (5.3). macOS 10.15 Catalina has the current zsh
5.7.1.
Is bash gone!?
No.
macOS Catalina still has the same /bin/bash
(version 3.2.57) as Mojave and earlier macOS versions. This change is only for new accounts created on macOS Catalina. When you upgrade to Catalina, a user’s default shell will remain what it was before.
Many scripts in macOS, management systems, and Apple and third party installers rely on /bin/bash
. If Apple just yanked this binary in macOS 10.15 Catalina or even 10.16. Many installers and other solutions would break and simply cease to function.
Users that have /bin/bash
as their default shell on Catalina will see a prompt at the start of each Terminal session stating that zsh
is now the recommended default shell. If you want to continue using /bin/bash
, you can supress this message by setting an environment variable in your .bash_profile
or .bashrc
.
You can also download and install a newer version of bash yourself. Keep in mind that custom bash installations reside in a different directory, usually /usr/local/bin/bash
.
Will bash remain indefinitely?
Apple is strongly messaging that you should switch shells. This is different from the last switch in Mac OS X 10.3 Panther, when Apple switched the default to bash
, but didn’t really care if you remained on tcsh
. In fact, tcsh
is still present on macOS.
Apple’s messaging should tell us, that the days of /bin/bash
are numbered. Probably not very soon, but eventually keeping a more than ten year old version of bash
on the system will turn into a liability. The built-in bash had to be patched in 2014 to mitigate the ‘Shellshock’ vulnerability. At some point Apple will consider the cost of continued maintenance too high.
Another clue is that a new shell appeared on macOS Catalina (and is mentioned in the support article). The ‘Debian Almquist Shell’ dash
has been added to the lineup of shells. dash
is designed to be a minimal implementation of the Posix standard shell sh
. So far, in macOS (including Catalina),sh
invokes bash
in sh
-compatibility mode.
As Apple’s support article mentions, Catalina also adds a new mechanism for users and admins to change which shell handles sh
invocations. MacAdmins or users can change the symbolic link stored in /var/select/sh
to point to a shell other than /bin/bash
. This changes which shell interprets scripts the #!/bin/sh
shebang or scripts invoked with sh -c
. Changing the interpreter for sh
should not, but may change the behavior of several crucial scripts in the system, management tools, and in installers, but may be very useful for testing purposes.
All of these changes are indicators that Apple is preparing to remove /bin/bash
at some, yet indeterminate, time in the future.
Mac Os Catalina
Do I need to wait for Catalina to switch to zsh?
No, zsh
is available Mojave and on older macOS versions. You can start testing zsh
or even switch your default shell already.
If you want to just see how zsh
works, you can just open Terminal and type zsh
:
The main change you will see is that the prompt looks different. zsh
uses the %
character as the default prompt. (You can change that, of course.) Most navigation keystrokes and other behaviors will remain the same as in bash
.
If you want to already switch your default shell to zsh
you can use the chsh
command:
This will prompt for your password. This command will not change the current shell, but all new ones, so close the current Terminal windows and tabs and open a new one.
How is zsh different?
Like bash
(‘Bourne again shell’ ), zsh
derives from the ‘Bourne’ family of shells. Because of this common ancestry, it behaves very similar in day-to-day use. The most obvious change will be the different prompt.
The main difference between bash
and zsh
is configuration. Since zsh
ignores the bash
configuration files (.bash_profile
or .bashrc
) you cannot simply copy customized bash settings over to zsh
. zsh
has much more options and points to change zsh
configuration and behavior. There is an entire eco-system of configuration tools and themes called oh-my-zsh
which is very popular.
zsh
also offers better configuration for auto-completion which is far easier than in bash
.
I am planning a separate post, describing how to transfer (and translate) your configurations from bash
to zsh
.
What about scripting?
Since zsh
has been present on macOS for a long time, you could start moving your scripts from bash
to zsh
right away and not lose backwards compatibility. Just remember to set the shebang in your scripts to #!/bin/zsh
.
You will gain some features where zsh
is superior to bash
v3, such as arrays and associative arrays (dictionaries).
There is one exception where I would now recommend to use /bin/sh
for your scripts: the Recovery system does not contain the /bin/zsh
shell, even on the Catalina beta. This could still change during the beta phase, or even later, but then you still have to consider older macOS installations where zsh
is definitely not present in Recovery.
When you plan to use your scripts or pkgs with installation scripts in a Recovery (or NetInstall, or bootable USB drive) context, such as Twocanoes MDS, installr or bootstrappr, then you cannot rely on /bin/zsh
.
Since we now know that bash
is eventually going away, the only common choice left is /bin/sh
.
When you build an installer package, it can be difficult to anticipate all the contexts in which it might be deployed. So, for installation pre- and postinstall scripts, I would recommend using /bin/sh
as the shebang from now on.
I used to recommend using /bin/bash
for everything MacAdmin related. /bin/sh
is definitely a step down in functionality, but it seems like the safest choice for continued support.
Summary
Overall, while the messaging from Apple is very interesting, the change itself is less dramatic than the headlines. Apple is not ‘replacing’ bash
with zsh
, at least not yet. Overall, we will have to re-think and re-learn a few things, but there is also much to be gained by finally switching from a ten-year-old shell to a new modern one!
Next
In the next part we will look at the configuration files for zsh
.
Bully Bash Mac OS