Wiki Update: Path in Shell Scripts

Submitted for your review, comment, and revision, a major update to this Wiki section:

###Path in Shell Scripts in the Execute Shell Script Action article.

###Background

  • First, my sincere thanks to @peternlewis, @Tom, and @ccstone for their review and changes to my initial draft.
  • In particular, @Tom identified the need to update this, and provided some great suggestions, long ago.
  • This is actually a new section in this article.
  • Paths in Shell Scripts are often misunderstood. We have had numerous topics/questions posted concerning this.
  • So, if you can, please try to review this from the eyes of a user who may have limited experience in shell scripts.
  • Finally, you should know I am a total novice at shell scripts. So I am totally relying on you guys to make sure all of this is technically correct, and is clear.

###Primary Changes:

  • ADDed a new section named "Path in Shell Scripts"
  • Clearly show the default path
  • Show how to set a path
  • Show how to use the KM Var "ENV_PATH"

###Remaining Issues:

  1. This paragraph now seems redundant to me. I propose it be removed:
    .

FIXME: [@JMichaelTX: I propose to remove the below paragraph since the system path is now provided above.]
The default PATH for the system will generally be /usr/bin:/bin:/usr/sbin:/sbin - that is the script will search for tools in the /usr/bin directory, then in the /bin directory, then /usr/sbin and finally /sbin. Only tools installed by the system will be in these directories - any tools you have installed will almost certainly be elsewhere and so not found unless you either configure the PATH environment variable within Keyboard Maestro (by setting the ENV_PATH Keyboard Maestro variable or explicitly setting the PATH environment variable within each Execute Shell Script action) or use the full path of the command (eg, /usr/local/bin/exiftool).
.

  1. Use of "ENV_PERL5LIB" is still not clear to me. Please fix:

If your tool requires other environment variables to be set you can set them as well by creating an appropriate Keyboard Maestro variable with ENV_ at the front.
For example, if you want to use Perl in a Execute Shell Script, you can also create a Keyboard Maestro Variable ENV_PERL5LIB that will FIXME [which? use OR be set to] the PERL5LIB environment variable for each Execute Shell Script.
FIXME [@JMichaelTX: This is still unclear to me. Does the ENV_PERL5LIB variable need to be set explicitly to a path? OR, does it somehow get the path from the PERL5LIB environment variable?]

1 Like

In my understanding ENV_PERL5LIB is not as important for Perl scripts as ENV_PATH is for Bash scripts.

Perl gets the paths of the modules folders from its @INC array. The @INC is specific for each Perl installation, and it is known to Perl no matter if you run the Perl script via KM or otherwise.

A use-case for ENV_PERL5LIB in KM:

‌
It is not uncommon that you have two or more Perl installations on your computer, for example:

  • The Perl that comes preinstalled with macOS (currently ver 5.18.2)
  • usually /usr/bin/perl; let’s call this System’s Perl
  • An up-to-date Perl (currently ver 5.24.1)
  • often /usr/local/bin/perl; let’s call this Main Perl
    ‌

So, let’s say I have installed the Pandoc module for my Main Perl installation.
‌

Now I run this script via KM:

#!/usr/local/bin/perl

use Pandoc;

This will throw no errors, since the path of the Pandoc module is in the @INC of my Main Perl.
‌

Then I run this script:

#!/usr/bin/perl

use Pandoc;

The System’s Perl will complain that it can’t find the Pandoc module, because it’s not part of its @INC:

Can't locate Pandoc.pm in @INC (you may need to install the Pandoc module)

‌

The safest thing would be to install the Pandoc module for the System’s Perl. (“safest” because not every module version works with every Perl version.)

But, I could also force the System’s Perl to look for the module at a specified path. I can do this either by saying:

#!/usr/bin/perl -I <path/to/a/module/folder>

use Pandoc;

Or – instead of the modified shebang – I could add the module’s path to the ENV_PERL5LIB variable in KM, which makes Perl add that path to its @INC array.
‌


Please note, I’m not a full-time Perl user, so it may be that there are other (or better) use-cases for the variable which are not obvious to me.

A good read is this Stackoverflow post.

I have removed most of the paragraph mentioned, and moved some of it above.

I have clarified the PERL5LIB variable usage.

PERL5LIB is just being used as an example of another environment variable you could configure. If you can think of a better one, I am happy to use it instead.

PERL5LIB lets you specify an alternative location for installed perl modules, so if you install your perl modules somewhere and are still using the system perl, you would use the PERL5LIB variable to specify where they are.

I have included a link to the perl docs that describes the PERL5LIB variable, but really its a bit off topic - its not about that specific environment variable, its just about explaining that this is not just for $PATH, you can set any other required environment variables as needed by your software.

OK, thanks Peter.

This section now looks very good, very thorough, and most important, very clear.
I guess the real test will be when another user has questions about the Path, and we point him/her to the Wiki.

Unless you guys have more questions or issues, I will now consider this topic closed.

Job well done, everyone! :+1:

1 Like