Where are functions stored in a Linux shell?

Bodisha

New Member
Joined
May 19, 2019
Messages
13
Reaction score
1
Credits
151
Thanks for any advice anyone can offer me on my question

Is there's a hash table where defined shell functions are kept? If so, what is it called?

So basically I'm wondering where in a shell (BASH) functions are stored. I know I can use the declare command to view them, but I suspect there is a hash table for them... but I can't find any reference to a memory structure that specifically holds them.

If anyone could tell me I would be grateful.
 


Is there's a hash table where defined shell functions are kept?
Functions? Or variables? I think you meant shell/environment variables. AFAIK, bash functions can be saved in .bashrc or .bash_profile or .profile. Also, you can create a functions.sh script file to keep your functions there, then source the file with .bashrc so you can call them from the shell/terminal. To create a function you just write it following certain parameters https://tldp.org/LDP/abs/html/functions.html If you meant variables, which I think you did, you can see the OS's with command
Code:
printenv
as to bash/shell, you can use set command to see which ones are currently being used. If you did mean something different, please give us an example.
 
Thanks for the reply.... My original question might have been a bit ambiguous... So let me try to clear that up

Functions? Or variables? I think you meant shell/environment variables.

I mean functions

bash functions can be saved in .bashrc or .bash_profile or .profile. Also, you can create a functions.sh script file to keep your functions there, then source the file with .bashrc so you can call them from the shell/terminal.

I know how to create a function, and where the shell reads them from. My question is where are they stored in the shell itself once they've been read.

Aliases are kept in the "alias hash table"... I'm guessing there has to be a similar structure for functions, but I can't find any reference to anything like this specifically for functions.

So i'm looking for the name of what holds a defined function in memory

Thanks for your original reply and if you can shed any light on my question
 
Most of them are simply in the binary.

strings bash

will give you about 12,000 lines of text. You'll see a lot of functions in there.
 
Aliases are kept in the "alias hash table"... I'm guessing there has to be a similar structure for functions, but I can't find any reference to anything like this specifically for functions. My question is where are they stored in the shell itself once they've been read.
Those "hash tables" you're talking about are stored as a binary structure in the shell's virtual memory for as long as the shell session remains open, and they are recreated each time you create a shell process. There are also bash-builtins commands which are contained within the shell itself; let, alias, bg, bind, break, case, cd, command, compgen, complete, continue, declare, dirs, disown, echo, enable, eval, exec, exit, export ... What functions are you specifically talking about? Can you give us an example? You might want to read here https://www.gnu.org/software/bash/manual/html_node/Bash-Features.html
 
Last edited:
Basically bash functions are permanently stored in a bash start-up script.
System-wide start-up scripts: /etc/profile for login shells, and /etc/bashrc for interactive shells.
User define start-up scripts: ~/.bash_profile for login shells, and ~/.bashrc for interactive shells.
More information on interactive/login shells can be found in the bash man page in the INVOCATION section.
They don't exist in any usable way until the shell starts (e.g., when you login to a CLI, or start a shell window such as xterm) and they are defined by reading .bashrc, .bash_profile, or something similar and they cease to exist when the shell terminates.
 
For some reason I'm not conveying my thoughts

I've very well aware functions are typically stored in a startup script like /etc/profile or /$HOME/.bashrc... And those defined functions are read into a shell's memory when a user logs in... What I'm attempting to identify is the name of the structure they are kept in after they've been read into memory

For example... aliases are defined in startup scripts... and are read into memory when a user logs in... and are stored in the "alias hash table"... which is in the same region of memory as the shell... I'm guessing there has to be a similarly named structure that holds functions

All I'm trying to understand is what's the name of the region of memory that holds the defined functions AFTER they are read into memory
 
What you are asking has to do with how bash is programmed. Since what you are looking for isn't found in the bash documention, you will either have to read the source-code to find out yourself and if you can't you will have to find someone who can and has enough knowledge of the code to explain it to you. What are you going to do with the information once you know how and where it is programmatically stored, what's your goal?
 

Members online


Latest posts

Top