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?

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
g4s8
  • 438
  • 6
  • 18
  • 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 Dec 29 '18 at 18:52
  • No, the _name_ of the function is a required part of the [function declaration syntax](https://www.gnu.org/software/bash/manual/html_node/Shell-Functions.html#Shell-Functions). Zsh has [anonymous functions] though, not that you asked about it. – ilkkachu Dec 29 '18 at 19:22
  • google `bash anonymous function` – jsotola Dec 29 '18 at 19:39
  • https://stackoverflow.com/questions/12299676/is-it-feasible-to-implement-anonymous-functions-in-bash-even-using-eval-if-nec – jsotola Dec 29 '18 at 19:47

0 Answers0