Re: [opensuse] bash pipe



On Tuesday 30 September 2008 00:19, Brian K. White wrote:
----- Original Message -----
From: "David Bolt" <bcrafhfr@xxxxxxxxxx>
...

This should do:

WHICHEVER_VARIABLE_NAME_YOU_WANT=${PWD##*/}

Ahh finally!
Phew I was wondering if _anyone_ was going to say anything other
than the unnecessary process fork of running basename.

I'd composed a message with this (and the dirname counterpart,
${PWD%/*}), but by the time I added all the caveats for the special
cases in which this differs from basename and dirname when you're in
the root directory or, for the dirname substitute, any directory whose
immediate parent is the root, I decided it wasn't worth it.

Here's what I wrote but didn't send at the time:

-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
On Monday 29 September 2008 06:11, drek wrote:
Hello,

In a script I want to know in which directory I am, but I don't want
to know the whole path. ...

Anyone with an explanation?

You've gotten that already, but I'll give you the most concise and
lowest overhead way to do what you want:

% echo "$PWD"
/home/rschulz/Mail

% echo "${PWD##*/}"
Mail


The reverse is possible, too:

% echo "${PWD%/*}"
/home/rschulz


However, the second one doesn't work well if you're in "/" or a
directory whose immediate parent is "/", in which case you get an empty
string. The first gives you an empty string if you're in "/", which may
or may not be acceptable for you. It is different than what basename and
dirname yield, certainly.
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-


--
Brian K. White


Randall Schulz
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx



Relevant Pages

  • dirname and basename errors
    ... I encountered a strange behavior of dirname and basename: ... basename shoud return an empty string IMHO. ... How can I obtain correct answers from them with a pathname without a real ...
    (comp.os.linux.misc)
  • Re: dirname and basename errors
    ... Remi Villatel wrote: ... | I encountered a strange behavior of dirname and basename: ...
    (comp.os.linux.misc)
  • Re: Command-line equivalent of "Create archive of..."
    ... Use basename and dirname to get just the filename and path respectively from a combined path/filename. ... The shell command that you need to execute is: ...
    (uk.comp.sys.mac)
  • Re: dirname and basename errors
    ... > basename shoud return an empty string IMHO. ... in a slash should be interpreted as if it had a dot after it). ... write your own functions to replace basename and dirname. ...
    (comp.os.linux.misc)
  • Re: Getting path components
    ... you can use dirname() and basename() in combination. ... also realpath() if you need it. ... basename from OpenSSH:- ...
    (comp.lang.c)