Running a Shell Script From Finder and Keeping the Filepath

If you’re a macOS user and want to run a shell script by double-clicking it in the Finder, you might encounter an issue with the working directory. By default, the working directory of the script becomes the user’s home directory, which can lead to unexpected behavior if your script relies on relative file paths. To ensure that your script runs with the correct working directory, you can include a specific command at the beginning of your script.

The Issue

When you double-click a shell script in the Finder, the script is executed, but the working directory is set to your home directory (~). This can cause problems if your script references files or paths that are relative to the location of the script itself.

The Solution

To ensure that your script runs with the correct working directory – the directory where the script is located – you can include a few lines of code at the beginning of your script. Here’s how you can do it:

1
2
3
4
5
6
7
8
#!/bin/bash

# Change the working directory to the location of the script
cd -- "$(dirname "$0")"

# Your script commands go here
# For example:
echo "Running script from $(pwd)"

Let’s break down what this code does:

  1. #!/bin/bash: This is known as a shebang line. It specifies the interpreter (in this case, Bash) that should be used to run the script.

  2. cd -- "$(dirname "$0")": This line changes the working directory to the location of the script. The "$0" refers to the path of the script itself, and dirname extracts the directory name from the path. The cd command then changes the directory to the script’s location.

  3. After the cd command, you can include your script’s actual commands. In the example above, we’ve included a simple echo command to demonstrate that the script is indeed running in the correct directory.

By including these lines at the beginning of your shell script, you ensure that it always runs with the correct working directory.

Implementation

  1. Open a text editor of your choice, such as TextEdit, Visual Studio Code, or Sublime Text.

  2. Copy and paste the code snippet mentioned above into the text editor.

  3. Replace the placeholder commands (echo "Running script from $(pwd)") with your actual script’s commands.

  4. Save the file with a .sh extension. For example, you could save it as myscript.sh.

  5. Open the Terminal and navigate to the directory where you saved the script.

  6. Make the script executable by running the following command in the Terminal:

    1
    
    chmod +x myscript.sh
  7. Close the Terminal.

  8. Now, when you double-click the myscript.sh file in the Finder, it will execute with the correct working directory.

Conclusion

Running a shell script by double-clicking it in the Finder can be convenient, but it’s important to ensure that the script runs with the correct working directory. By adding a few lines of code to change the directory to the script’s location, you can avoid unexpected issues related to file paths and ensure that your script behaves as expected.


Feel free to customize the example and instructions based on your specific use case and preferences. Just make sure to include the necessary steps for creating and running the shell script with the correct working directory.

0%