How can I match a string with a regex in Bash?

后端 未结 6 1158
囚心锁ツ
囚心锁ツ 2020-12-02 05:04

I am trying to write a bash script that contains a function so when given a .tar, .tar.bz2, .tar.gz etc. file it uses tar with the rel

6条回答
  •  借酒劲吻你
    2020-12-02 05:12

    Since you are using bash, you don't need to create a child process for doing this. Here is one solution which performs it entirely within bash:

    [[ $TEST =~ ^(.*):\ +(.*)$ ]] && TEST=${BASH_REMATCH[1]}:${BASH_REMATCH[2]}
    

    Explanation: The groups before and after the sequence "colon and one or more spaces" are stored by the pattern match operator in the BASH_REMATCH array.

提交回复
热议问题