Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

bash expansion question

Options
  • 26-05-2009 4:01pm
    #1
    Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭


    Can some explain this to me please?

    path=/home/user/filename.txt
    echo ${path##*/}

    prints "filename.txt".

    This is exactly what I want, but I have no idea how or why it works and I'm a curious cat.


Comments

  • Registered Users Posts: 4,287 ✭✭✭NotMe


    Here.. from 'man bash':
    ${parameter#word}
    ${parameter##word}
    The word is expanded to produce a pattern just as in pathname
    expansion. If the pattern matches the beginning of the value of
    parameter, then the result of the expansion is the expanded
    value of parameter with the shortest matching pattern (the ‘‘#’’
    case) or the longest matching pattern (the ‘‘##’’ case) deleted.

    so it's $path with the longest matching pattern for */ (any number of chars followed by a / ) deleted.


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    Fair play to ye. Didn't think to check the man pages tbh. Just didn't expect it to be in there. It's quite the read.


  • Registered Users Posts: 3,730 ✭✭✭E39MSport


    echo "`basename $path`" does the same thing if you are looking to remove the path and print the file name only. You can also use `basename $ path .txt` to print the filename without the file extension.


  • Moderators, Technology & Internet Moderators Posts: 37,485 Mod ✭✭✭✭Khannie


    The bash substitution is very powerful. Been reading up / fluting with it today.


Advertisement