bash shell gives weird result when directory exists vs not

后端 未结 2 639
粉色の甜心
粉色の甜心 2021-01-25 20:51

Alright, I\'ve wasted a full day on a script that was behaving very oddly. I have found out something that I can\'t explain.

ny-02-ops:~$ rm -fr roles
ny-02-ops         


        
相关标签:
2条回答
  • 2021-01-25 20:58

    The default behaviour of the shell is to expand a glob where possible, otherwise return it unchanged. There are ways to change this behaviour, however.

    To expand non-matching globs to an empty string (useful for for loops which you may just want to skip if no files match, for example), use nullglob:

    shopt -s nullglob
    

    If the fact that the glob doesn't expand is considered an error, use failglob:

    shopt -s failglob
    

    Use shopt -u to unset these shell options.

    0 讨论(0)
  • 2021-01-25 21:09

    Square brackets in a glob match one of the characters inside. If a glob with metacharacters (*, [...], etc.) matches no filenames then the glob is returned unchanged.

    echo "role[sugar]"
    echo "role[blerk]"
    
    0 讨论(0)
提交回复
热议问题