2

As the title states, I'd like determine what shell (cmd, bash, or etc) was used to run the node.js script (from within the script).

While detecting the OS with the os module can give some clues, it is not enough is some instances, such as as if I were executing from bash on win32 through a terminal like GitBash or Cygwin.

Is there a property somewhere on the process object that has this information?

*Edit: While process.env.SHELL is an option, it is not always populated and maybe undefined when using some terminals

Mofi
  • 46,139
  • 17
  • 80
  • 143
strider
  • 5,674
  • 4
  • 24
  • 29
  • Can you get the parent process id (PPID) in node.js? Might be related: http://stackoverflow.com/questions/5541288/detect-when-parent-process-exits – lit Apr 01 '17 at 22:47
  • What problem are you actually trying to solve by getting this information? What are you going to do with this information if/when you know it? – jfriend00 Apr 02 '17 at 02:01
  • So specifically what i'm trying to do is add support for single quoted string args from a `cmd` shell, starting with detection. @lit, how could one use a PPID to discover the shell? – strider Apr 02 '17 at 04:29
  • I have recently seen a command that will show what program started a process, but cannot recall it. You can see it in Process Explorer. If I can find it, I will get back to you. – lit Apr 02 '17 at 19:15

1 Answers1

2

You can use the process.env.SHELL property to get that as seen in the official documentation,

The process.env property returns an object containing the user environment. An example of this object looks like:

{
  TERM: 'xterm-256color',
  SHELL: '/usr/local/bin/bash',
  USER: 'maciej',
  PATH: '~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin',
  PWD: '/Users/maciej',
  EDITOR: 'vim',
  SHLVL: '1',
  HOME: '/Users/maciej',
  LOGNAME: 'maciej',
  _: '/usr/local/bin/node'
}
Inian
  • 80,270
  • 14
  • 142
  • 161
  • Unfortunately `process.env.SHELL` is not guaranteed to be populated. With some terminals, like Window Command Prompt, it is `undefined` – strider Apr 01 '17 at 21:19