On Wed, 1 Oct 2008, 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?
Randall has given you an explanation about what it does. As for where I came across it, initially I saw some mention of it in a book[0] I have, but didn't think much of it. Later I saw others suggesting similar constructs as solutions, so looked at the bash man page and reread that part of the book again. After doing so I've found that some of the useful, but unused by me, parameter expansions can come in handy. One of these useful expansions is this: SOME_VAR=${SOME_VAR:-value} which will assign the value "value" to SOME_VAR if it's empty, or leaves it alone if it's not. This would mean that you can get the exact same behaviour of basename by using: MY_DIR=${PWD##*/} MY_DIR=${MY_DIR:-/} or dirname using: MY_DIR=${PWD%/*} MY_DIR=${MY_DIR:-/} Just to give you a bit more insight, here's a quick comparison: davjam@adder:/local2/possible-viruses> MY_DIR="${PWD##*/}" ; MY_DIR="${MY_DIR:-/}" ; echo "${MY_DIR}" $(basename "${PWD}") possible-viruses possible-viruses davjam@adder:/local2/possible-viruses> davjam@adder:/local2/possible-viruses> MY_DIR="${PWD%/*}" ; MY_DIR="${MY_DIR:-/}" ; echo "${MY_DIR}" $(dirname "${PWD}") /local2 /local2 davjam@adder:/local2/possible-viruses> davjam@adder:/local2/possible-viruses> pushd / / /local2/possible-viruses davjam@adder:/local2/possible-viruses> davjam@adder:/> MY_DIR="${PWD##*/}" ; MY_DIR="${MY_DIR:-/}" ; echo "${MY_DIR}" $(basename "${PWD}") / / davjam@adder:/local2/possible-viruses> davjam@adder:/> MY_DIR="${PWD%/*}" ; MY_DIR="${MY_DIR:-/}" ; echo "${MY_DIR}" $(dirname "${PWD}") / / [0] Beginning Linux Programming, 3rd edition. ISBN 0-7645-4497-7 Regards, David Bolt -- Team Acorn: http://www.distributed.net/ OGR-P2 @ ~100Mnodes RC5-72 @ ~15Mkeys SUSE 10.1 32 | | openSUSE 10.3 32b | openSUSE 11.0 32b | openSUSE 10.2 64b | openSUSE 10.3 64b | openSUSE 11.0 64b RISC OS 3.6 | TOS 4.02 | openSUSE 10.3 PPC | RISC OS 3.11 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org