Find the answer to your Linux question:
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 ...
  1. #1
    Linux 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

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So that particular notation takes a variable (in your case, $PWD), and does a longest-substring removal on it. The basic notation is:
    Code:
    ${VAR##substring}
    So if we have the following:
    Code:
    var='abcabcd'
    echo "${var#a*c}" # produces 'abcd'
    echo "${var##a*c}" # produces 'd'
    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.

    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.html
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Newbie
    Join Date
    Nov 2006
    Posts
    123
    Quote Originally Posted by Cabhan
    So that particular notation takes a variable (in your case, $PWD), and does a longest-substring removal on it. The basic notation is:
    Code:
    ${VAR##substring}
    So if we have the following:
    Code:
    var='abcabcd'
    echo "${var#a*c}" # produces 'abcd'
    echo "${var##a*c}" # produces 'd'
    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.

    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.html

    ah, i see -that's perfect, cheers.

  4. #4
    Just Joined! fatalexception's Avatar
    Join Date
    Feb 2007
    Posts
    34
    Quote Originally Posted by bentaylor
    what mechanism is this using though, regular expressions? or what?
    Cabhan 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.

    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:
    Code:
    entity@domina:~$ player=`basename /usr/bin/mplayer`
    entity@domina:~$ echo $player
    mplayer
    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.

    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:
    Code:
    for i in {1..10}; do echo "$i"; done
    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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
        #etc
    done
    which kinda creeps me out.

    Have fun.

  5. #5
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    oooo... i like that

  6. #6
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Quote Originally Posted by fatalexception
    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'.
    Minor quibble: the integer feature of brace expansion came with bash version 3:
    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
    Produces:
    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}e
    cheers, drl (who looked over http://www.network-theory.co.uk/docs...Expansion.html and immediately installed bash3)
    Welcome - 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 )

  7. #7
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    O no wonder I hadn't seen that, my machines are running 2.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...