Anonymous functions in shell scripts

前端 未结 6 1711
無奈伤痛
無奈伤痛 2021-01-03 20:00

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 thi

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-03 20:55

    The common technique is to assign function definitions conditionally:

    #!/bin/sh
    
    case $1 in
    a) foo() { echo case a; };;
    b) foo() { echo case b; };;
    *) foo() { echo default; } ;;
    esac
    
    foo
    

提交回复
热议问题