Unix Shell - :t and getting senile.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2006 > :t and getting senile.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author :t and getting senile.
george.e.sullivan@saic.com

2006-09-25, 7:34 pm

Ok, I know I am getting old. I can't remember where I saw this, but
how do you strip off the end of string such as a command line string so
that you just get the last part after the slash bar?

Ex: /here/there/everywhere/hello.

There was something I saw somewhere with a :t that would just give me
the hello part.

Thanks to all.

Chris F.A. Johnson

2006-09-25, 7:34 pm

On 2006-09-25, george.e.sullivan@saic.com wrote:
> Ok, I know I am getting old. I can't remember where I saw this, but
> how do you strip off the end of string such as a command line string so
> that you just get the last part after the slash bar?
>
> Ex: /here/there/everywhere/hello.
>
> There was something I saw somewhere with a :t that would just give me
> the hello part.


I don't know about :t (that may be something non-standard, such as
csh or zsh), but in a POSIX shell:

var=/here/there/everywhere/hello
base=${var##*/}

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Stephane CHAZELAS

2006-09-25, 7:34 pm

2006-09-25, 12:22(-07), george.e.sullivan@saic.com:
> Ok, I know I am getting old. I can't remember where I saw this, but
> how do you strip off the end of string such as a command line string so
> that you just get the last part after the slash bar?
>
> Ex: /here/there/everywhere/hello.
>
> There was something I saw somewhere with a :t that would just give me
> the hello part.

[...]

That's a csh feature also found in tcsh and zsh. That comes from
history substitution. bash implemented csh history substitution
but didn't go as far as that.

$file:r (rest (without extension)) $file:h (head), $file:t
(tail).

With bash it's only available via the csh-like history
substitution:

It can also be found in vim. :cd %:h changes vim current
directory the the directory part of the currently edited file.

bash-3.1$ echo a/b.c
a/b.c
bash-3.1$ echo !$:t
echo b.c
b.c

bash copied the bogus ${array:index:length} from ksh that makes
it not-really-compatible with ${var:modifier}. zsh used the more
sensible ${var[start,end]}, so was able to borrow the
$var:modifier from csh.

$var:h in zsh gives the dirname even for the cases where
${var%/*} fails such as when var=foo (not with csh or tcsh).
Same for $var:t vs ${var##*/} when var=/foo/.

--
Stéphane
Stephane Chazelas

2006-09-26, 7:32 am

On Mon, 25 Sep 2006 21:23:23 +0100, Stephane CHAZELAS wrote:
[...]
> $var:h in zsh gives the dirname even for the cases where
> ${var%/*} fails such as when var=foo (not with csh or tcsh).
> Same for $var:t vs ${var##*/} when var=/foo/.


I forgot to mention that with zsh, they can also be used in the
globbing qualifiers, as globbing modifiers.

Like:

printf '%s\n' /etc/**/*(:t)

prints the *names* (not paths) of all the files under /etc.

--
Stephane
george.e.sullivan@saic.com

2006-09-26, 1:22 pm


Stephane Chazelas wrote:
> On Mon, 25 Sep 2006 21:23:23 +0100, Stephane CHAZELAS wrote:
> [...]
>
> I forgot to mention that with zsh, they can also be used in the
> globbing qualifiers, as globbing modifiers.
>
> Like:
>
> printf '%s\n' /etc/**/*(:t)
>
> prints the *names* (not paths) of all the files under /etc.
>
> --
> Stephane



Thanks all. Yes...yes...yes. That is it. I can't believe how
easily I forgot that. Arrrgghhh.. Thank you all for reminding me. I
know have a sticky not.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com