bash script yields a different result when sourced

后端 未结 1 1402
谎友^
谎友^ 2021-01-18 14:18

Could you help me, why this script works when sourced (or even directly on console) and does not work on a script?

I have checked and in any case I\'m using the same

相关标签:
1条回答
  • 2021-01-18 14:41

    Bash does not recognize +(pattern) syntax unless extglobs are enabled, and they are disabled by default. Apparently your bash setup enables them in interactive sessions; that's why your script works only when sourced in an interactive shell.

    To fix that, either enable extglobs within the script by this command:

    shopt -s extglob
    

    Or use an alternative that works irrespective of shell's interactiveness:

    bname=$(sed 's/__*[0-9][0-9]*\.fit$//' <<< $fname)
    # with GNU sed it'd look like:
    bname=$(sed -E 's/_+[0-9]+\.fit$//' <<< $fname)
    
    0 讨论(0)
提交回复
热议问题