在bash腳本中,我想從給定的字符串中提取變量字符串。我的意思是,我倒是想從一個字符串中提取字符串file.txt
:帶正則表達式的Bash子串
This is the file.txt from my folder.
我想:
var=$(echo "This is the file.txt from my folder.")
var=echo ${var##'This'}
...
,但我倒是喜歡,使其在一個更清潔的方式,使用expr
, sed
或awk
命令。
感謝
編輯:
我找到了另一種方式(不過,與sed命令的回答是最好的一個對我來說):
var=$(echo 'This is the file.txt from my folder.')
front=$(echo 'This is the ')
back=$(echo ' from my folder.')
var=${var##$front}
var=${var%$back}
echo $var