Script Identity Crisis – get the Path to the Current Script

In shell scripting, there are many situations where you want to know the path to the script file itself.

I use this frequently in scripts that build pkgs, because I want the script to access or create files relative to the script or its enclosing folder. In other words: the script sits in some project folder, and you want to get the path to that project folder.

Some scripts just use the current working directory, but since you can launch scripts from outside the current working directory, this is quite unreliable.

One example is this script from my book ‘Packaging for Apple Administrators‘ which builds an installer package for a desktop picture from resources in the same folder.

In that package build script I have the following line:

projectfolder=$(dirname "$0")

This is a useful but somewhat tricky line. It will work in most situations, but not all. To understand when it might fail, we will have to unravel it from the inside.

The $0 variable is the first element of the command line as it is entered in to the prompt and contains the command itself. In our case it will most likely be ./buildBoringDesktopPkg.sh.

The dirname command removes the last element from a path. This will return the path to the directory containing the item you give it. When we do dirname $0 after launching the script with ./buildBoringDesktopPkg.sh we will get . which is the current working directory, but also the parent directory of the script.

When you launch the build script with a different working directory, i.e. like

> Projects/BoringDesktop/buildBoringDesktopPkg.sh

The $0 argument will be Projects/BoringDesktop/buildBoringDesktopPkg.sh the dirname will return Projects/BoringDesktop/. This is all good and it will still find the resources and create the pkg file in its project folder.

In most situations the syntax used to call your script will be a relative or absolute path to the script. Something like ./buildPkg.sh or ~/Projects/Tools/my_script.sh. When the shell ‘sees’ a slash / in the command, it will assume this is the path to the script or executable and use that.

But when there is no slash / in the command, the shell will use the directories listed in the PATH environment variable to look for the command. This is the reason you can type sw_vers instead of having to write /usr/bin/sw_vers.

This is also the reason you have to type ./my_script.sh to run a script you are working on. Your script is not in the PATH, but the command contains a slash and tells the shell exactly where to look (in the current directory). When you just type my_script.sh the shell will look in the PATH directories, not find your script, and error out. (Unless your current directory happens to be in the PATH.)

Note: Learn the details about PATH in my new book: “macOS Terminal and Shell

So, there are a lot of pieces that are really confusing here. Let’s create a script that will help us visualize this:

#!/bin/sh

echo "pwd: $(pwd)"
echo "\$0: $0"
echo "dirname \$0: $(dirname $0)"

This script shows the values of the current working directory, the $0 argument, and the value returned by dirname $0. Save this script as script_path_test.sh in your Desktop folder.

> cd Desktop                      
> ./script_path_test.sh     
pwd: /Users/armin/Desktop
$0: ./script_path_test.sh
dirname $0: .
> cd ~                  
> Desktop/script_path_test.sh     
pwd: /Users/armin
$0: Desktop/script_path_test.sh
dirname $0: Desktop

So far, so good. While some of these paths look a little odd, they all return the expected values.

But now imagine we like our script so much, that we use it a lot. To save on typing the path to the script all the time, we add a symbolic link to it to /usr/local/bin. This puts our script in the default PATH (for an interactive shell) and we can now execute it without needing to type the path to it:

> sudo ln -s ~/Desktop/script_path_test.sh /usr/local/bin/script_path_test.sh
> script_path_test.sh
pwd: /Users/armin
$0: /usr/local/bin/script_path_test.sh
dirname $0: /usr/local/bin

So, this is less ideal. When you enter just script_path_test.sh the shell finds the symbolic link in /usr/local/bin and runs it. The $0 argument points to the symbolic link instead of the actual script and hence dirname $0 points to /usr/local/bin which is not where the resources for our script are.

(Remember to delete this symbolic link from /usr/local/bin/ when you are done with this.)

We need a tool that resolves symbolic links.

There are quite a few solutions to this, many of which can be found in this Stack Overflow post.

GNU-based systems have a realpath command line tool, which does exactly this. macOS does not have this tool. You can add GNU tools to macOS with many of the popular package managers, but that introduces a dependency and seems overkill.

Python has a function that does this. Add this line to the script:

echo "python realpath: $(python -c "import os; print(os.path.realpath('$0'))")"

This works fine, however, the built-in Python for macOS is deprecated. We don’t want to introduce this dependency, when it might go away in a future macOS update.

Then I found this post where I learned that zsh has a parameter expansion modifier to return the absolute path while resolving symbolic links.

Let’s modify our script to use zsh:

#!/bin/zsh

echo "pwd: $(pwd)"
echo "\$0: $0"
echo "dirname \$0: $(dirname $0)"

echo "\${0:A}: ${0:A}"
echo "dirname \${0:A}: $(dirname ${0:A})"

and then we can run our script:

> script_path_test.sh
pwd: /Users/armin
$0: /usr/local/bin/script_path_test.sh
dirname $0: /usr/local/bin
${0:A}: /Users/armin/Desktop/script_path_test.sh
dirname ${0:A}: /Users/armin/Desktop

Zsh to the rescue. We have a solution! Zsh is pre-installed on every macOS and since Apple chose it as their default shell since Catalina, it will be around for a while longer.

But what if you have sh or bash scripts that you cannot just quickly convert to zsh? The good news here is that we can create a zsh one-liner that does this, even from a bash or sh script, very similar to the python one-liner above.

echo "zsh absolute path: $(zsh -c 'echo ${0:A}' "$0")"
echo "zsh dirname absolute: $(dirname $(zsh -c 'echo ${0:A}' "$0"))"

So, in conclusion:

  • dirname $0 will give you the enclosing folder of the script in most situations
  • with zsh you can just use dirname ${0:A} to be even safer
  • when you have to use sh or bash, you can use a zsh one-liner: dirname $(zsh -c 'echo ${0:A}' "$0").

Happy Scripting!

(Remember to delete the symbolic link from /usr/local/bin/!)

Weekly News Summary for Admins — 2021-03-19

Fairly quiet week. No major news from Apple, except for the fourth beta of macOS 11.3, iOS 14.5 and siblings.

Apple has admitted that the original ‘large’ HomePod is discontinued. You can still buy them while supplies last. This seems like release some bad news before the good news. Apple often releases new products or updates at this time of year.


To support the weekly news summary, please consider:

macOS Terminal and Shell Book Cover

macOS Terminal and Shell:
You have always wanted to ‘learn Terminal,‘ right? This book teaches how (and why) to use the command line on macOS. Get it on Apple Books!


I have a list of upcoming conferences and dates on my website. The list also contains links where you can submit a session proposal and a link to the video archive of the conference, where available (this is new since last week).

PS: Don’t be alarmed. I will be experimenting with the layout of this newsletter over the next few weeks.

📰News and Opinion

🌅macOS 11 Big Sur and Apple silicon Macs

🐦MacAdmins on Twitter

  • Tim Perfitt: “I think I finally figured out how DFU in the M1 works. Apple says to: 1. Plug in to correct USB port to another mac running AC2. 2. hold power, left control option and right shift for 10 seconds 3. the release all but power button for another 10 seconds.” (Thread)

🔐Security and Privacy

🔨Support and HowTos

🤖Scripting and Automation

♻️Updates and Releases

📺To Watch

🎧To Listen

📚 Support

If you are enjoying what you are reading here, please spread the word and recommend it to another Mac Admin!

EraseInstall application retired

It makes me very sad that the EraseInstall application has been retired.

We built this tool three years ago, mostly because we wanted to learn how to build an app like this on macOS. We think it worked out well. We learned a lot, and are glad the application was useful to some.

Since then, all the people involved in the EraseInstall Project have moved on to other jobs or other responsibilities. Unfortunately, this leaves us with no time or resources to maintain or improve EraseInstall.

The repository and the application will remain available in its current state. There will be no more updates. If someone feels they can take up the project and continue it, please do!

If you are looking for a similar solution, we recommend Graham Pugh’s eraseinstall script.

Thank you, all, again!

Team EraseInstall: Mischa van der Bent, Arnold Nefkens, and Armin Briegel

Weekly News Summary for Admins — 2021-03-12

Lots of news this week as macOS 11.2.3 and a iOS 14.4.1 dropped. Adobe is starting to ship Apple silicon versions of their software, but they are not universal apps. Git has a security issue with git clone, but no updated installer for macOS.

And I was on the MacAdmins Podcast and MacAdmins Monthly!

PSU MacAdmins have announced their schedule for another series of Campfire sessions this summer and MacDevOps YVR are calling for proposals! I have put the list I started last week on a page on my site. I plan to update that regularly.

If you would rather get the weekly newsletter by email, you can subscribe to the Scripting OS X Weekly Newsletter here!! (Same content, delivered to your Inbox once a week.)

News and Opinion

macOS and iOS Updates

MacAdmins on Twitter

  • Steve Troughton-Smith: “If you were confused by the matrix of SwiftUI, Catalyst and AppKit apps on macOS, it’s because this is what it looks like:” (Image)
  • Darren Wallace: “Mac Adobe Users seeing a new folder at ~/Documents/Library that you do not have permission to? Looks like a bug introduced for users who’s Creative cloud Desktop App (CCDA) updated from v5.3 > v5.4. Adobe are aware and a hot-fix is being worked on.”
  • Jason Broccardo: “#macadmins: Apple, please stop releasing supplemental updates that don’t change the OS build number as it creates confusion. Apple: Sure thing, we’ll require an OS update to patch Safari. Build number changes just like you asked.”
  • Nathaniel Strauss: “AutoPkg recipes updated for Google Drive 46.0.3. (AutoPkg recipes) Google is starting to ship Intel and Apple silicon versions side by side. Expect packages to be roughly double in size (~300 MB) with both included.”

Security and Privacy

Support and HowTos

Scripting and Automation

Updates and Releases

To Watch

To Listen

Support

If you are enjoying what you are reading here, please spread the word and recommend it to another Mac Admin!

If you want to support me and this website even further, then consider buying one (or all) of my books. It’s like a subscription fee, but you also get a useful book or two extra!

Weekly News Summary for Admins — 2021-03-05

Jamf is opening the registration for the virtual JNUC 2021! They are also calling prospective speakers to submit their proposals.

Because I was curious what the status of the various Mac Admin conferences was this year, I put together a list.

The list is for information only. I do not intend to shame or pressure any of the organziers of these conferences, no mattter what the status of their conference is. These are harrowing times for organizing anything, let alone a conference. My deepest thanks and respect to anyone who puts together any form of conference or meeting.

Registration open

Not certain yet

Please let me know if missed something and I will update it next week!

If you would rather get the weekly newsletter by email, you can subscribe to the Scripting OS X Weekly Newsletter here!! (Same content, delivered to your Inbox once a week.)

News and Opinion

macOS and iOS Updates

MacAdmins on Twitter

  • Mr. Macintosh: “macOS Big Sur 11.2.2 (20D80) Full Installer is now available.”
  • Wil Shipley: “Honestly if you’re distributing software that requires me to have either python or cmake (or, worse, both cough openCV) you’ve failed everyone. Anyways, the next couple hours I’ll be upgrading python and downloading modules, because that’s a fun game that I love playing.”

Security and Privacy

Support and HowTos

Scripting and Automation

Apple Support

Updates and Releases

To Listen

Just for Fun

Support

If you are enjoying what you are reading here, please spread the word and recommend it to another Mac Admin!

If you want to support me and this website even further, then consider buying one (or all) of my books. It’s like a subscription fee, but you also get a useful book or two extra!

Book Update for Big Sur – Moving to zsh v5

I have pushed an update for the “Moving to zsh” book.

Since I have also published a new book “macOS Terminal and Shell” last month, you might be wondering whether you need both books, or just one.

Moving to zsh” is the book where I documented my journey from using bash in Terminal on macOS to using zsh. Before Apple announced that they would switch from bash to zsh as the default shell with macOS Catalina, I used bash “because it was the deafult.” In this book, I describe how to move from bash to zsh. Because of this, “Moving to zsh” is aimed at a user who is already conformtable using Terminal with bash and is wondering what the change means and how to get some extra features and productivity out of zsh.

macOS Terminal and Shell” is the book for those that have no or little experience with using Terminal and probably don’t even know why bash or zsh matters. Or maybe you have a bit experience, but just don’t feel comfortable because you have the feeling you are not quite understanding what is going on. This book will teach you to use Terminal and the shell with confidence, and it will show how you can configure it to be more productive. Since zsh is the current default shell on macOS Catalina and Big Sur, we will focus on zsh, but explain differences to bash where necessary.

As usual, the update to “Moving to zsh” is free if you have already purchased the book. You should get a notification from the Books application to update. (On macOS, I have seen that it can help to delete the local download of the book to force the update.)

If you are enjoying the book, please rate it on the Books store, or even leave a review. These really help, thank you!

The changes are listed here. This list is also in the ‘Version History’ section in the book. There, you will get links to the relevant section of the book, so you can find the changes quickly.

  • Updated list of other books with ‘macOS Terminal and Shell’
  • Added the vared command (variable editor) as an alternative to read
  • Many typos and other minor corrections and clarifications

Weekly News Summary for Admins — 2021-02-26

Lots of updates this week: a new app for cloud logins, Munki patch and beta, a tool to ‘Nudge’ users to update or upgrade, an update to Scout, which reads plists, xml and JSON, and Jamf Connect.

Also another minor update for macOS. It is refreshing that Apple now has the third version digit available to distinguish ‘supplemental updates.’ Less refreshing is the fact that even a ‘minor’ update which fixes a single documented issue requires a 2.6GB download and 30 minutes with multiple reboots.

If you would rather get the weekly newsletter by email, you can subscribe to the Scripting OS X Weekly Newsletter here!! (Same content, delivered to your Inbox once a week.)

macOS 11 Big Sur and Apple silicon Macs

macOS and iOS Updates

MacAdmins on Twitter

  • Tim Perfitt: “ok, this was unexpected. you can run custom gui apps and command utils on the m1 in 1TR (recovery).” (Thread)
  • Greg Neagle: “@tperfitt It should. Bootstrappr works fine on m1 from 1TR/Recovery.”
  • Laura Rösler: “After 3 months with macOS Big Sur, almost ¾ of our Mac fleet is on macOS 11.”

Security and Privacy

Support and HowTos

Scripting and Automation

Apple Support

Updates and Releases

To Watch

To Listen

Just for Fun

  • MIT CSAIL: “Today’s the day that “hello world” said “hello world!” The term was coined in a textbook published #otd in 1978: “C Programming Language,” written by Brian Kernighan and Dennis Ritchie.”

Support

If you are enjoying what you are reading here, please spread the word and recommend it to another Mac Admin!

If you want to support me and this website even further, then consider buying one (or all) of my books. It’s like a subscription fee, but you also get a useful book or two extra!

Weekly News Summary for Admins — 2021-02-19

Yesterday evening (central european time) my social media feeds were dominated with news and comments about the Perserverance Mars rover landing on Mars. I watched the live stream and it was thrilling to watch the culmination of years of work of thousands of experts.

As I was watching the live stream from the JPL command center on the big screen and following the comments and updates on Twitter, I realized how wonderful it is that I can follow and celebrate such an event live, from my home.

Before internet streaming and social media, the details of such an event would have been inaccessible to all but a few. Sadly, this kind of event is not really of interest for TV stations. We would have gotten a nice summary the next day in the news. The scientists and engineers behind this miraculous achievement would have remained faceless.

We would have missed the drama of an automated spacecraft landing itself so far away, that any signal of success or disaster takes 11.5 minutes to reach Earth. We would have missed the anxiety and trepidation of the people in the control center, watching the telemetry for even the most minute signs of success or failure. We would have missed the increasing relief and celebration as each milestone was successfully handled by the automated systems designed, built, programmed, and tested over years and years. We would have missed the humanity of the event.

But now, millions of interested viewers, all over the world, can tune in. The engineers get their well-deserved moment on the stage and you can get a glimpse of how human the entire endeavor is during the final “seven minutes of terror,” where so much could go wrong, with instant disastrous consequences, but amazingly did not!

It has become apparent, that there are many problems with modern media that need to be addressed and solved. But it is a relief that there are some aspects that work and that we can really feel good about.

Of course, for the actual research on Mars, the landing is just the beginning. So, well done, and happy rock-hunting, Perserverance and team!

The only thing that did bubble up through the Mars landing was MacAdmins who were really thrilled about the update to Apple’s Platform Security document. While we MacAdmins are often justifiably upset with the state or lack of Apple documentation, this is one area where they get it right, and that has to be acknowledged. Thank you, anonymous Apple writers!

If you haven’t gotten it yet, my new book “macOS Terminal and Shell” is available in the Apple Books store!

If you would rather get the weekly newsletter by email, you can subscribe to the Scripting OS X Weekly Newsletter here!! (Same content, delivered to your Inbox once a week.)

📰News and Opinion

🐦MacAdmins on Twitter

  • MacAdmins Conference: “The @macadminsconf conference team is prepping the fire pit for another summer of campfire sessions. Stay tuned for details on our virtual conference this year. #macadmins #psumac”
  • Mr. Macintosh: “macOS Big Sur 11.2.1 (20D75) Full Installer now available!!! 12.21GB 071-05432 Build number (20D75) vs 11.2.1 update (20D74)” (Fixes the free disk space issue)
  • Thomas Tempelmann: “Huh, diskutil coreStorage encryptVolume, which used to be the command to encrypt HFS volumes, isn’t available in BS any more. So, while my previous statement isn’t incorrect, the real reason is that BigSur removed the ability to encrypt HFS+ vols altogether. Nice one, Apple.”
  • Howard Oakley: “So heartening to see that even malware authors have released their first Universal binaries. That must leave just Adobe then?”

🔐Security and Privacy

🔨Support and HowTos

🤖Scripting and Automation

🍏Apple Support

♻️Updates and Releases

🎧To Listen

🎈Just for Fun

📚 Support

If you are enjoying what you are reading here, please spread the word and recommend it to another Mac Admin!

If you want to support me and this website even further, then consider buying one (or all) of my books. It’s like a subscription fee, but you also get a useful book or two extra!

Weekly News Summary for Admins — 2021-02-12

Publication day! “macOS Terminal and Shell” is available! You can still get the introductory pricing US$/€17.99 (10% off) until Feb 15, after which the standard price will be US$/€19.99.

If you would rather get the weekly newsletter by email, you can subscribe to the Scripting OS X Weekly Newsletter here!! (Same content, delivered to your Inbox once a week.)

News and Opinion

macOS 11 Big Sur and Apple silicon Macs

macOS and iOS Updates

MacAdmins on Twitter

  • Mr. Macintosh: “Just because you can update the DTK to 11.3 Beta, doesn’t mean you should. Upgrading to 11.3 will disable both USB-A ports with firmware update 6723.100.321. You can downgrade to 11.2 using AC2. Once back on 11.2, USB-A will work again. fw = 6723.81.1”
  • Steve Hayman: “Well, ok. So, officially I was shayman@next.com. The boss was of course sjobs@next.com . There were 8 Steves at the company at the time… .” (Thread)
  • Mr. Macintosh: “macOS Apple Silicon M1 Restore Database Updated! Big Sur 11.2.1 IPSW restore file is now available. UniversalMac_11.2.1_20D74_Restore.ipsw Quick Note: 11.0.1 was still signed as of last night.”
  • Victor: “FYI: Many macOS MDM profiles are “single use” and have no effect after they’re installed. This is especially true of profiles introduced in recent years like system extension/privacy approvals etc.” (Thread)

Bugs and Security

Support and HowTos

Scripting and Automation

Apple Support

Updates and Releases

To Listen

Just for Fun

Support

If you are enjoying what you are reading here, please spread the word and recommend it to another Mac Admin!

If you want to support me and this website even further, then consider buying one (or more) of my books. It’s like a subscription fee, but you also get a useful book or two extra!