3

When creating Bash scripts, I have always had a line right at the start defining the PATH environment variable. I recently discovered that this doesn't make the script very portable as the PATH variable is different for different versions of Linux (in my case, I moved the script from Arch Linux to Ubuntu and received errors as various executables weren't in the same places).

Is it possible to copy the PATH environment variable defined by the login shell into the current Bash script?


EDIT: I see that my question has caused some confusion resulting in some thinking that I want to change the PATH environment variable of the login shell with a bash script, which is the exact opposite of what I want.

This is what I currently have at the top of one of my Bash scripts:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
# Test if an internet connection is present
wget -O /dev/null google.com

I want to replace that second line with something that copies the value of PATH from the login shell into the script environment:

#!/bin/bash
PATH=$(command that copies value of PATH from login shell)
# Test if an internet connection is present
wget -O /dev/null google.com

EDIT 2: Sorry for the big omission on my part. I forgot to mention that the scripts in question are being run on a schedule through cron. Cron creates it's own environment for running the scripts which does not use the environment variables of the login shell or modify them. I just tried running the following script in cron:

#!/bin/bash
echo $PATH >> /home/user/output.txt

The result is as follows. As you can see, the PATH variable used by cron is different to the login shell:

user@ubuntu_router:~$ cat output.txt
/usr/bin:/bin
user@ubuntu_router:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
XJDHDR
  • 494
  • 5
  • 15
  • 3
    why do you want to do this ? – Bertrand Martel Jul 23 '16 at 04:06
  • 1
    On Mac OS X, the `/etc/profile` contains: `if [ -x /usr/libexec/path_helper ]; then eval `/usr/libexec/path_helper -s`; fi`. This sets `PATH` and `MANPATH`. You can find questions about `path_helper` on U&L and SU — the speed issue mentioned has been resolved (a few — as in 8-10 — milliseconds on 10.11.6). Look in `/etc/paths.d` perhaps on Linux? But the question stands — why do you want to tinker with the PATH? I might have my own views about which versions of the commands should be used; that's what my PATH settings are for. Of course, if I break your script, the onus is on me to fix it. – Jonathan Leffler Jul 23 '16 at 05:13
  • I'm afraid the two of you have misunderstood what I was requesting. I have edited my question to hopefully clear things up. – XJDHDR Jul 23 '16 at 12:58
  • 1
    No, you are not understanding what these comments are telling you. What you are asking for is poor behavior on the part of your program, and seems to be based on a false assumption. – tripleee Jul 23 '16 at 13:08
  • *All* processes inherit a value for `PATH` from their parents. You don't have to do anything special. If you need proof, put `echo $PATH` as the very first line of your script. – chepner Jul 23 '16 at 13:47
  • I have edited my question again. I just realized that there was a very important piece of information that I forgot to mention. I'm terribly sorry. – XJDHDR Jul 23 '16 at 14:59

2 Answers2

5

Don't touch the user's PATH at all unless you have a specific reason. Not doing anything will (basically) accomplish what you ask.

You don't have to do anything to get the user's normal PATH since every process inherits the PATH and all other environment variables automatically.

If you need to add something nonstandard to the PATH, the usual approach is to prepend (or append) the new directory to the user's existing PATH, like so:

PATH=/opt/your/random/dir:$PATH

The environment of cron jobs is pretty close to the system's "default" (for some definition of "default") though interactive shells may generally run with a less constrained environment. But again, the fix for that is to add any missing directories to the current value at the beginning of the script. Adding directories which don't exist on this particular system is harmless, as is introducing duplicate directories.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • You have misunderstood my question. I don't want to modify the `PATH` environment variable. I want to copy the value of that environment variable into the script's environment. – XJDHDR Jul 23 '16 at 12:42
  • I believe I understand you perfectly; if you do nothing, you will inherit the user's `PATH`, which in most cases will be identical to the system's default `PATH`, and if it isn't, the user most likely has a good reason to have configured it that way. Anyway, it's not necessary for there to be a single system-wide default, though it's common practice e.g. on Linux. If you do feel the need to override the user's configured preference, the mechanism I describe is what you should probably do instead of what you seem to want. – tripleee Jul 23 '16 at 13:03
  • I have edited my question again. I just realized that there was a very important piece of information that I forgot to mention. I'm terribly sorry. – XJDHDR Jul 23 '16 at 14:59
  • I basically still stand by this answer, though I updated it briefly with some `cron` remarks. – tripleee Jul 23 '16 at 15:16
1

I've managed to find the answer to my question:

PATH=$PATH:$(sed -n '/PATH=/s/^.*=// ; s/\"//gp' '/etc/environment')

This command will grab the value assigned to PATH by Linux from the environment file and append it to the PATH used by Cron.

I used the following resources to help find the answer:

How to grep for contents after pattern?

https://help.ubuntu.com/community/EnvironmentVariables#System-wide_environment_variables

XJDHDR
  • 494
  • 5
  • 15