Cons
Cons

Reputation: 1193

Bash / Substring of a string

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?

Upvotes: 2

Views: 127

Answers (3)

anubhava
anubhava

Reputation: 785156

You can also do:

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

OUTPUT:

30

Live Demo: http://ideone.com/bKHGcS

Upvotes: 2

jaypal singh
jaypal singh

Reputation: 77105

$ n=${b%/*}
$ n=${n#*-}
$ echo $n
30

Upvotes: 2

Ziffusion
Ziffusion

Reputation: 8923

Try this.

b="try-30/16"
n=${b##*-}
n=${n%%/*}
echo $n

Upvotes: 4

Related Questions