Find the answer to your Linux question:
Results 1 to 2 of 2
I can't for the life of me figure out modifiers in bash. If someone could help me out that would be swell. Basically I'm trying to do a few things, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    15

    modifiers in bash


    I can't for the life of me figure out modifiers in bash. If someone could help me out that would be swell. Basically I'm trying to do a few things, none of which are working. Basically I have a filename that I'm saving to a variable. Then I want to just get the ending (everything but the suffix) of that file. In a separate command it would also be useful to change the suffix. So I have something like:

    Code:
    test="filename.com"
    out="$test:r.o"
    end="$test:e"
    echo $out
    echo $end
    I usually get something like:

    Code:
    filename.com:r.o
    filename.com:e
    as the output. This works perfectly in zsh but I just can't seem to get it to work in bash. There should be a way to do this right? I've been able to get the ending with this:

    Code:
    echo "$test" | awk -F . '{print $NF}'
    but it would be much easier to do with modifiers and I feel like it should be possible. Anyone have any suggestions?

  2. #2
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    If I understand you correctly try this:

    Code:
    echo ${out#*.}
    echo ${end#*.}
    man bash for details. It's in the Parameter Expansion section.

    Code:
           ${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.
                  If parameter is @ or *, the pattern removal operation is applied
                  to each positional parameter in turn, and the expansion  is  the
                  resultant  list.   If parameter is an array variable subscripted
                  with @ or *, the pattern removal operation is  applied  to  each
                  member  of the array in turn, and the expansion is the resultant
                  list.

Posting Permissions

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