-   Programming (
-   -   BASH: How to get current workin directory? (

gmitra 09-15-2003 07:20 AM

BASH: How to get current workin directory?
is anyone who could give me an advice how to check the correctness of current working directory? Not by $PWD which
writes the full path, just the name of directocty in which you are.

Thanks. Gmitra.

druuna 09-15-2003 07:45 AM

Would this help:



echo "Normal pwd : ${PWD}"
echo ""
echo "Stripped pwd : ${PWD##/*/}"

gmitra 09-15-2003 08:20 AM

line PWD="'pwd'" sets PWD to string pwd
but working PWD=$PWD

Where can I find more info about tricks like this ${PWD##/*/}?

gmitra 09-15-2003 08:27 AM

But I would like to set some variable say WD to stripped path.

druuna 09-15-2003 09:36 AM


line PWD="'pwd'" sets PWD to string pwd
but working PWD=$PWD
PWD="'pwd'" is not what I wrote, that should be "`pwd`"
ie: not single quotes, but the other one (sorry, don't know the english name).

More info/'tricks':

- man bash (might be a bit cryptic at times)
- (Linux Shell Scripting Tutorial)
- any book about bash/ksh.

The variable name you choose is free (well, almost. There are some reserved names).

If you want to fill a variable with a certain value:
variablename=value (variable on the left and value on the right)
So WD="foo" will assign foo to the variable WD.

The PWD="`pwd`" (or WD="`pwd`") is a bit more complicated.

The PWD (or WD) part is the name of the variable, nothing strange here.

pwd is a command, so WD="`pwd`" will be interpreted as follows (command line processing):

1) pwd will be executed,
2) `pwd` will be substituted by the result of 1
3) WD will be filled with result of 2

Hope this helps a bit.

gmitra 09-15-2003 10:55 AM

I have been found possible solution:

echo "${WDir##/*/}" > $$.tmp
WDir="`cat $$.tmp`"
rm $$.tmp
echo $WDir

` = back quote

Thank you druuna very much.

Hko 09-15-2003 11:21 AM

No need for the temp-file:

echo $WDir

Or just:

echo $WDir

gmitra 09-15-2003 11:48 AM

Its great!
What does mean ##*/ ?
Or where I can find more info?

Hko 09-15-2003 07:06 PM

A=${B##*/} means: assign to variable A the string contained in variable B, but without the longest part from the start that matches the wildcard */

The wildcard works the same way as in " ls *.txt ". The asterisk (*) means "any number of any character", so " */ " means "any number of any character, followed by a slash ( / )".

So ${B##*/} means "B with the first part removed, up to, and including the last slash.

And ${B#*/} (1 single '#') means "B with the first part removed, up to, and including the first slash.

Examples: given B="/usr/local/doc/somefile.txt"

A=${B##*/} : A = "somefile.txt"
A=${B#*/} : A = "usr/local/doc/somefile.txt"

'%' and '%%' do the opposite: they remove from the end.

A=${B%%/*} : A = "" (empty, everything removed)
A=${B%/*} : A = "/usr/local/doc"

Note that because these remove from the end, the asterisk ( * ) and the slash are swapped. Otherwise nothing would happen. Do you see why?

Like druuna already said you can find this info, and also some more of these "tricks" in "man bash".

mfeat 09-20-2003 10:30 PM

WDir=`basename $PWD`

All times are GMT -5. The time now is 11:15 AM.