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

Spread the love


Advertisement

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

Advertisements

Testapp.app/
├─ Contents/
│  ├─ MacOS/
│  │  ├─ Testapp

The contents of Testapp is

#!/bin/bash

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

The contents of test.sh 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 test.sh 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 test.sh 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 *