Bob Bob - 8 days ago 6
Bash Question

How can I get the local shell variables in C?

I'm currently working on my own replica of the history builtin.
I realize I need the HISTSIZE, HISTFILE and HISTFILESIZE variables to achieve the command options but I don't know how to get them. Is there a function similar to getenv or a variable like environ but with the local shell variables ?

Thanks in advance !

Answer

Well a builtin is executed directly by the shell for 2 possible reasons:

  • you avoid spawning a new processus (performance reason)
  • it is the only way to have access to internal shell variables (your use case)

A shell could only provide access to its internal variables by two ways:

  • either copy them to the environment because by definition the environment is the list of variables that are passed to child processes
  • either provided an inter process communication mechanism (named pipe, private sockect, etc.) to allow its childs to ask them

AFAIK, no common shell implement either, but you can export the required shell variables to the environment:

export HIST
export HISTSIZE
export HISTFILESIZE

should be enough to later get them through getenv