On Tuesday 30 September 2008 15:57, drek wrote:
On 09/29/2008 04:02 PM, David Bolt wrote:
This should do: WHICHEVER_VARIABLE_NAME_YOU_WANT=${PWD##*/} ^ ^ And these are curly braces.
Thanks, but... Can you explain this? For me, it is just a magic guess with some #'s and a star and slash. How do you come up with this solution?
Well, it's well-known among BASH-o-philes. It's a general mechanism for expanding a variable while simultaneously performing some pattern-directed rewriting on the expanded value (leaving the variable's value unchanged). # strips from the beginning, % from the end. A single # or % strips a minimal match (when a * is involved, other patterns or strings having a fixed length) while a double ## or %% strips a maximal match. There's also a generalized pattern editing version using (can you guess?) the slash character, '/'. Another answer to your question is: It's all in the (lengthy) BASH manual page and covered in better books on BASH scripting. I'd venture to guess that the most used case is the simple #-style prefix strip. I say this because when processing options in BASH scripts, one often has something like this: for arg; do case "$arg" in -o) optionO=1 ;; --foo=*) fooVal="${arg#--foo=}" ;; --bar=*) barVal="${arg#--bar=}" ;; -*) echo "Unknown option: \"$arg\"" >&2 eval errorCount++ ;; *) nonOptionArgs=( "${nonOptionArgs[@]}" "$arg" ) ;; esac done
André
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org