How echo ${instance##*/} expand by shell?

Posted on 2011-10-17
Last Modified: 2013-12-26
Hello, I am trying to understand following in bash scripts

[user@hostname]$ ls -ld /opt/product/jms/instances/etc/ac4-*
drwxr-xr-x  2 user company 4096 Oct 15 17:18 /opt/product/jms/instances/etc/ac4-sfae-dev
drwxr-xr-x  2 user company 4096 Oct 10 21:08 /opt/product/jms/instances/etc/ac4-sfae-prod
drwxr-xr-x  2 user company 4096 Oct 15 17:09 /opt/product/jms/instances/etc/ac4-sfae-stage
[user@hostname]$ for instance in /opt/product/jms/instances/etc/ac4-*; do echo ${instance##*/}; done

Open in new window

here I would like to understand how ${instance##*/} is being expanded. I was thinking to use 'basename' but ${instance##*/} also serve my purpose. Though this is the first time I saw this variable so interested to know how does it work. Thanks!
Question by:beer9
    LVL 9

    Accepted Solution

    The word (*/ in this case) is expanded to produce a pattern. If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the longest matching pattern (the ‘##’ case) deleted.
    (Therefore the whole path including the last / was deleted and leaving only the file names.)

    Refer to for more details about bash commands.
    LVL 31

    Assisted Solution

    # means cut from the left side (first instance)
    % means cut from the right side (first instance)
    ## cut from left side but the last instance
    %% cut from left side but the last instance

    Now say
    > a='/opt/product/jms/instances/etc/ac4-sfae-dev'
    > echo ${a#*/}              ---- Chops off the first instance of / and the rest is printed.

    > echo ${a##*/}    --matches any characters * and then should also match slash, all the match is chopped off from the left side

    Now lets chop off the first instance of / from the right side.
    > echo ${a%/*}

    And the last instance of / from the right side
    > echo ${a%%/*}
           NO OUTPUT --everything deleted.

    Now suppose we had
    > echo ${b%%/*}


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now