Is it possible to create something analogous to an anonymous function whose value can be assigned to an array element and later called? I can't seem to find a way to do this in a bash script but perhaps there's a workaround.
相关问题
- How to get the return code of a shell script in lu
- JQ: Select when attribute value exists in a bash a
- Invoking Mirth Connect CLI with Powershell script
- Emacs shell: save commit message
- bash print whole line after splitting line with if
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- Check if directory exists on remote machine with s
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
If you really need array to store the functions, you can define named functions and store just their names. You can then call the function as
${array[n]}
. Or, you can name themfunc1
..funcN
and then just callfunc$n
.well bash is turing complete, soo thats perfectly possible ;)
but aside from this its not really worth the consideration.
you could simulate such behaviour though with something along this line:
but why?!?
It is possible; I wrote a library to do exactly this, though it's a very strange project. The source code is available at http://github.com/spencertipping/bash-lambda. Using this library:
The trick is to have the
fn
function create a file containing the body of the function,chmod +x
that file, then return its name. This causes stray files to accumulate, which is why the library also implements an asynchronous mark/sweep garbage collector.Create the fn file in your
PATH
You can then do :
Short answer: No.
Long answer: Nooooooooooooo.
Complete answer: Functions in bash are not first-class objects, therefore there can be no such thing as an anonymous function in bash.
The common technique is to assign function definitions conditionally: