1

I have a script file (it's .bashrc actually), in this file I have a lot of variables and functions which are used only inside this script, but these variables and functions are publicly visible for any bash session. To avoid collisions, make me (as a shell user) to understand that this variables are internal and avoid unnecessary autocompletion I added a prefix _bashrc_ for each variable and functions, e.g. _bashrc_extensions, but such prefixes looks ugly for me.

The idea I have in mind is to reduce scope of visibility to be able to remove these prefixes, but the only way I know in bash is to use local keyword, which can be used only inside a function. So I'm thinking about a function without a name which will be called immediately, something like this:

function {
  local path=$1
  # do something with $path
} "$HOME/.mypath"

Does something similar exist in bash?

4
  • No, but you can give it a name and then use unset -f to remove the name as the link provided by steeldriver suggests.
    – icarus
    Commented Dec 29, 2018 at 18:52
  • No, the name of the function is a required part of the function declaration syntax. Zsh has [anonymous functions] though, not that you asked about it.
    – ilkkachu
    Commented Dec 29, 2018 at 19:22
  • google bash anonymous function
    – jsotola
    Commented Dec 29, 2018 at 19:39
  • stackoverflow.com/questions/12299676/…
    – jsotola
    Commented Dec 29, 2018 at 19:47

0

Browse other questions tagged .