Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use ${parameter#word} and ${parameter%word} to remove prefix and postfix of string.

$ x=2012-hello.jpg
$ y=${x#*-}
$ z=${y%.*}
$ echo $z
hello

How can I do them at once instead of two.

share|improve this question
1  
I don't think that's possible. – Daniel Beck Jan 7 '12 at 15:24
up vote 4 down vote accepted

It is not possible with parameter expansion, but it is possible with regular expression matching:

x=2012-hello.jpg
[[ $x =~ .*-(.*)\.jpg ]] && echo ${BASH_REMATCH[1]}
share|improve this answer

If you are OK with using sed instead of parameter expansion:

x=2012-hello.jpg
$ z="$(sed -e 's/.*-\(.*\)\..*/\1/' <<<"$x")"
$ echo "$z"
hello
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .