所以我們的目標是用一個提供的正則表達式替換部分路徑名。 我想使用內置的正則表達式替代品$ {//路徑源/目標}Bash替換/ /不處理正則表達式的錨?因爲它是像glob一樣的
/tmp/some\/where/ is somewhat crude, as my_tmp would be matched to stupidity
/\/tmp\//\/somewhere\// is better
/^\/tmp\//\/somewhere\// is the best, but this last one doesn't seem to work.
一個簡單的例子,因爲那些反斜槓是殺手:
$ t2="there hello"
$ t1="hello there"
$ echo ${t1//hello/goodbye}
goodbye there
$ echo ${t2//hello/goodbye}
there goodbye
但是讓我們說我只想要開始招呼:
$ echo ${t2//^hello/goodbye}
there hello -- as required
$ echo ${t1//^hello/goodbye}
hello there -- but not what I want here
-- ok let's try oldschool
$ echo ${t2//~hello/goodbye}
there hello -- as required
$ echo ${t1//~hello/goodbye}
hello there -- but not what I want here
$ t3="^hello there"
$ echo ${t3//^hello/goodbye}
goodbye there --^is just a character :-(
但是對於=〜,我聽到你問...
$ if [[ $t1 =~ ^hello ]] ; then echo yes ; else echo no ; fi
yes
$ if [[ $t2 =~ ^hello ]] ; then echo yes ; else echo no ; fi
no
$ if [[ $t3 =~ ^hello ]] ; then echo yes ; else echo no ; fi
no
所以,基本上=〜之間的行爲差異$ {//
我想我可以以某種方式使用=〜和$ BASH_REMATCH?
使用SED我得到預期的答案,但我真的不希望調用外部):
$ echo $t1 | sed s/^hello/goodbye/
goodbye there
$ echo $t2 | sed s/^hello/goodbye/
there hello
$ echo $t3 | sed s/^hello/goodbye/
^hello there
GNU的bash,版本48年3月4日(1)-release下(x86_64-Linux PC的-gnu)
你實際上是試圖取代一個正則表達式,或只是一個固定字符串(例如你好)?此外,雙斜線替換('$ {var // pattern/replacement}')替換了所有的事件,但是隻有一個錨點,所以單斜槓替換(只是第一次出現)更有意義。你想要做什麼? –
我想我已經從路徑示例中明確了。在這種特殊情況下,我只想要與前面相匹配的替換品。 但是,通過命令行傳遞它的想法是由用戶決定的。 –