Skip to content
Advertisement

Linux C++ Detect user shell (csh,bash,etc)

I have a C++ application that needs to make shell specific commands using system calls. Is there a way to detect which shell the user is running? (Csh/Bash/etc).

Thanks


Elaborate

I’m trying to work with some code that forks off via system a rsh call that has a sequence of commands that are using setenv which doesnt work in bash. I want to detect whether the system is csh or bash and rewrite the calls accordingly.

Advertisement

Answer

Dont know if this will be of any use

#include <iostream>
#include <cstdlib>     /* getenv */

int main ()
{
  char* Shell;
  Shell = getenv ("SHELL");
  if (Shell!=NULL)
  std::cout << Shell << std::endl;
  return 0;
}

Will output something similar to

/bin/bash

Getenv returns a c-string with the environment variables value.

Link:http://www.cplusplus.com/reference/cstdlib/getenv/

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement