2

When I use the "run shell script" in automator, and do, for example

say $PATH

it then tells me the PATH. But the path is incomplete. Some items I have in $PATH when using the terminal (zsh) are missing.

How can I fix this, so I have the same $PATH variable everywhere?

2
  • You can't - running from Finder/desktop does not read your zsh startup files. See many questions here. You have to explicitly source them in your script you run from automator
    – mmmmmm
    Commented Feb 21, 2023 at 10:36
  • @mmmmmm But it does have part of my PATH, so it must be possible to add my paths somewhere to make this work.
    – Ma Joad
    Commented Feb 21, 2023 at 10:51

3 Answers 3

2

There are two different shells - interactive and non-interactive. They behave just like they are described - interactively and non-interactively.

If you set your path in .zprofile, for example, anything you do in Terminal will have the path you set. However, a non-interactive shell (like when run from Automator) won’t read .zprofile so any variables like PATH won’t be set. I suggest using .zshenv or setting the path manually in your shell script.

See ZSH: .zprofile, .zshrc, .zlogin - What goes where? for a full description of what should go where when setting paths.

1

Automator's shell script action does not configure additional paths (such as /usr/local/bin) into the script's environment variable. To get these additional paths configured, make a call to the path_helper utility.

The following is an example snipped for a bash script to configure additional paths into the scripts' environment. Insert this near the top of your shell script action.

if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi
0

As Allan said, the shell environment variables, such as $PATH, are typically set in the shell configuration files (e.g., ~/.bashrc or ~/.zshrc). However, when you use Automator's "run shell script" action, it does not necessarily load your shell configuration files, which may result in a different $PATH variable than what you have in your terminal.

To ensure that you have the same $PATH variable in Automator's "run shell script" action as in your terminal, you can explicitly set the $PATH variable in your script. You can do this by adding the following line at the beginning of your shell script:

export PATH="$PATH:/your/additional/path/here"

This command sets the $PATH variable to the existing value of $PATH (represented by "$PATH") plus any additional directories you want to add (separated by colons).

Alternatively, you can source your shell configuration file at the beginning of the script by adding the following line:

source ~/.zshrc

This command sources your ~/.zshrc file, which will set the $PATH variable to its proper value.

By doing either of these, you can ensure that your "run shell script" action in Automator has the same $PATH variable as your terminal.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .