Is there any mechanism in Shell script alike “include guard” in C++?

前端 未结 3 730
一向
一向 2021-02-06 04:55

let\'s see an example: in my main.sh, I\'d like to source a.sh and b.sh. a.sh, however, might have already sourced b.sh. Thus it will cause the codes in b.sh executed twice. Is

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-06 05:17

    In bash, an early return does not affect the sourcing file, it returns to it as if the current file were a function. I prefer this method because it avoids wrapping the entire content in if...fi.

    if [ -n "$_for_example" ]; then return; fi
    _for_example=`date`
    

提交回复
热议问题