2

I try to find a substring of a string.

If I have the string try-30/16, I want to get the 30 string.

so I wrote the next:

n=${"'$b'":'-':'/'}

where $b is a variable I assign before this command. It gives the next:

bad substitution.

How I can do it?

4

3 回答 3

4

尝试这个。

b="try-30/16"
n=${b##*-}
n=${n%%/*}
echo $n
于 2013-06-10T14:56:43.333 回答
2

你也可以这样做:

[[ $b =~ [^-]+-([0-9]+) ]] && echo "${BASH_REMATCH[1]}"

输出:

30

现场演示:http: //ideone.com/bKHGcS

于 2013-06-10T15:06:24.423 回答
2
$ n=${b%/*}
$ n=${n#*-}
$ echo $n
30
于 2013-06-10T14:57:23.757 回答