Find the answer to your Linux question:
Results 1 to 3 of 3
I'm new to bash scripting. I been using a nifty tool to get the basename and extention for a filename. Example: file=blarg.txt echo ${file%.*} > blarg echo ${file##*.} > txt ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Location
    New Hampshire
    Posts
    4

    ${file##*.} and ${file%.*} explanation?

    I'm new to bash scripting. I been using a nifty tool to get the basename and extention for a filename.

    Example:

    file=blarg.txt
    echo ${file%.*}
    > blarg
    echo ${file##*.}
    > txt

    And of course I can replace the "." for any character or string. This is great for paths too.

    However, I don't really understand the syntax as it is cryptic. And when my friends ask for an explanation, I just . Does anyone know what's going on with the ##* and %.*? Is there a terminology I can use to google a tutorial or more tools.

    Thanks a head of time!
    ~Kat

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    It's about bash parameter expansion, it's built-in.

    Search for 'Remove matching prefix pattern' and 'Remove matching suffix pattern'

    There are many more parameter expansion.

    man bash
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Here's a good description of it: parameter substitution
    Can't tell an OS by it's GUI

Posting Permissions

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