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, ...
- 03-16-2011 #1Just 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:
I usually get something like:Code:test="filename.com" out="$test:r.o" end="$test:e" echo $out echo $end
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:filename.com:r.o filename.com:e
but it would be much easier to do with modifiers and I feel like it should be possible. Anyone have any suggestions?Code:echo "$test" | awk -F . '{print $NF}'
- 03-17-2011 #2Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
If I understand you correctly try this:
man bash for details. It's in the Parameter Expansion section.Code:echo ${out#*.} echo ${end#*.}
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.


Reply With Quote