I would like to extract the 2nd field from the last part of the following strings:foo_1.103.debug_xx.ver21-inc-1          --> string extracted debug_xxfoo_1.103.1.0.release_32_xx.ver21-inc-1 --> string extracted release_32_xxfoo_1.103.1.0.release_xx.ver21-inc-1    --> string extracted release_xxAny help will be appreciated.
bash string extraction
bash;shell script
Use ${VAR%PATTERN} to remove the suffix corresponding to the last field, then ${NEWVAR##PATTERN} to remove the prefix corresponding to all but the last remaining field.all_but_last_field=${mystring%.*}second_to_last_field=${all_but_last_field##*.}You need to store the string in a variable and store the intermediate result in a variable as well, you can't directly chain the expansions (you can in zsh, but not in bash).