Results 1 to 7 of 7
hello,
I found an example of how to get the last part of the current directory in bash by using
${PWD##*/}
which does just happen to work...what mechanism is this ...
- 03-23-2007 #1Linux Newbie
- Join Date
- Nov 2006
- Posts
- 123
modifying variables in bash
hello,
I found an example of how to get the last part of the current directory in bash by using
${PWD##*/}
which does just happen to work...what mechanism is this using though, regular expressions? or what?
And is there a reference list for commands like that?
Basically I want to know how that was done and why it works, in order to be able to do other things like that as and when necessary.
Thanks
- 03-23-2007 #2
So that particular notation takes a variable (in your case, $PWD), and does a longest-substring removal on it. The basic notation is:
So if we have the following:Code:${VAR##substring}
The first form finds the shortest substring from the front (the first 'abc') and removes it. The second finds the longest substring from the front (the 'abcabc') and removes it.Code:var='abcabcd' echo "${var#a*c}" # produces 'abcd' echo "${var##a*c}" # produces 'd'
The best resource for Bash scripting is the Advanced Bash Scripting guide:
http://www.tldp.org/LDP/abs/html/
I found your particular question at:
http://www.tldp.org/LDP/abs/html/str...ipulation.htmlDISTRO=Arch
Registered Linux User #388732
- 03-23-2007 #3Linux Newbie
- Join Date
- Nov 2006
- Posts
- 123
Originally Posted by Cabhan
ah, i see -that's perfect, cheers.
- 03-23-2007 #4Cabhan explained it correctly, but in case you're searching for the correct terminology when referring to this specific feature of Bash, it's known as one of many forms of 'parameter expansion'. Search for parameter expansion in the Bash manpage - there are tons of goodies in there I didn't know about. Bash is actually a huge language with myriads of syntax - probably next to impossible to fit the entire feature set in your head.
Originally Posted by bentaylor
There is another, perhaps more traditional way to accomplish the very same task you described, however. There is a common utility known as 'basename' which, when sent a path name, will output only the name of the file without any 'directory components'. A small but useful example:If you're relying heavily on this utility in a program, of course, you might want to stick to your syntax (unless remembering it is giving you a headache), as it is likely to incur a lot of overhead as your operating system has to create a process every time you use it.Code:entity@domina:~$ player=`basename /usr/bin/mplayer` entity@domina:~$ echo $player mplayer
This is completely unrelated, but here's one of those 'goodies' I was referring to. I believe it's called a 'sequence expression', and is a form of 'brace expansion'. It's used like this:This code loops 10 times, assigning $i to the number every time it is incremented. The only reason I bring this up is that I still see (yes, in Bash) code like this:Code:for i in {1..10}; do echo "$i"; donewhich kinda creeps me out.Code:for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do #etc done
Have fun.
- 03-26-2007 #5
- 03-26-2007 #6Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Minor quibble: the integer feature of brace expansion came with bash version 3:
Originally Posted by fatalexception
Produces:Code:#!/bin/bash # @(#) s2 Examine sequence expression. echo echo " This version of bash is:" bash --version echo echo a{d,c,b}e echo echo a{1..3}e
cheers, drl (who looked over http://www.network-theory.co.uk/docs...Expansion.html and immediately installed bash3)Code:% ./s2 This version of bash is: GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu) Copyright (C) 2002 Free Software Foundation, Inc. ade ace abe a{1..3}eWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 03-26-2007 #7
O no wonder I hadn't seen that, my machines are running 2.


Reply With Quote