0

I have a script that has:

global_npm_modules_path="$(npm root -g)";

this npm command is slow, and I am looking to speed up this script. One thing I could do is "cache" the result of the npm command, something like this:

export global_npm_modules_path=${global_npm_modules_path:-"$(npm root -g)"}

but my question is - how can I set global_npm_modules_path so that it's held in store by the parent shell / parent process?

The problem is the export command will only be relevant for this shell and its children.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 3
    Child process has its own copy of the environment, you can't set/modify parent's env vars from the child. – randomir Oct 16 '17 at 19:44
  • * cannot modify the parent's env from child? – Alexander Mills Oct 16 '17 at 19:45
  • 1
    Let the parent process set the variable, or have the child cache the value in a file – that other guy Oct 16 '17 at 19:47
  • yeah it's for a library, I will have to recommend to my users that they set this variable in bashrc or whatever. – Alexander Mills Oct 16 '17 at 19:48
  • 1
    @AlexanderMills, look at how programs like `ssh-agent` do it -- they instruct the user to `eval "$(ssh-agent -s)"`. If there were a prettier way to do things (that didn't break the security model), it would be in use in common/standard UNIX tools with comparable needs. – Charles Duffy Oct 16 '17 at 19:54
  • @CharlesDuffy thanks what does that eval command accomplish though? How is it different than just running ssh-agent directly? – Alexander Mills Oct 16 '17 at 19:56
  • 1
    @AlexanderMills, the `eval` command tells the shell to run the output from `ssh-agent` as a series of shell commands. Thus, it enables `ssh-agent` to modify the environment of the shell that calls it (so other children of that shell can use environment variables it exports to find the control socket for that process). – Charles Duffy Oct 16 '17 at 19:57
  • That sounds like a workaround for the builtin-security... "Yes, just run whatever commands my script outputs -- run them as root... Ignore the `rm -rf` / on your screen as it's running..." – HardcoreHenry Oct 16 '17 at 20:12
  • 1
    Maybe what you want to do is to save the variable in a .config file somewhere in your system, and restore that value when you run the program, rather than modify the environment... – HardcoreHenry Oct 16 '17 at 20:16
  • 1
    Write a function all the scripts source. Have it run the command and cache the output to a file somewhere if the file is older than $whatever, otherwise use what's in it. Salt to taste and warm as needed. – Paul Hodges Oct 16 '17 at 20:24
  • @PaulHodges that might work, thanks – Alexander Mills Oct 16 '17 at 20:36
  • @HardcoreHenry, ...need I point out that if you're running a subprocess it has all the permissions you do, with or without an `eval`? Avoiding `eval` is certainly good practice, because generating code is prone to accidental errors (some of which can enable malicious or badly-formed data to act as a vehicle for injection attacks); but if the program you're running is malicious and you aren't sandboxing it, you're in a bad state regardless. – Charles Duffy Oct 16 '17 at 21:27
  • @CharlesDuffy -- Sorry my example was bad. Yes, you are right, a malicious program could call `rm` directly. That being said the program could issue some commands (not just updating data) which would be very useful for steeling passwords, etc, that a standalone program would not be able to do. – HardcoreHenry Oct 17 '17 at 20:11

0 Answers0