macos – How can a bundled app bash script source files outside user’s home directory?

Spread the love


So I created a macOS bundle application and found some interesting behavior.
A MWE application structure looks like this:

├─ Contents/
│  ├─ MacOS/
│  │  ├─ Testapp

The contents of Testapp is


# echo "hello world" > $HOME/Desktop/test.txt  # works
# source $HOME/  # works
source $HOME/Documents/  # does not work

The contents of is

echo "hello world" > $HOME/Desktop/test.txt

When launching the application from the Finder, ~/Desktop/test.txt is created with text “hello world”, except if the file is not in the user home directory. Interestingly, this is not an issue if the shabang is changed from #!/bin/bash to #!/bin/sh; however my ultimate use case requires bash and I cannot use sh.

So, my questions are:

  1. Why does bash behave this way with macOS applications (but sh does not)?
  2. How do I fix it?

I've tried changing permissions on to 777 (from 644) and removing extended attributes (xattr -c) without any effect.

I'm running macOS 12.5.1 with Intel processor.

Author: Subham

Leave a Reply

Your email address will not be published. Required fields are marked *