0

hey guys I'm wondering if there is a smart way to look back before a period in bash

file='foo'/bar/styles.css?ver=1.4.2

ext=$(echo ${file} | gawk -F  "?" '{print$1}')
echo "${ext##*.}" # css

Seems like I should be able to do this all in my expansion somehow?

ehime
  • 8,025
  • 14
  • 51
  • 110
  • I'm not quite sure of the goal here, but look at the `basename` command and see if it helps you with what you are trying to do. Otherwise, please provide an explanation of the expected input and the result. – Ned Jan 14 '14 at 00:12
  • 1
    what exactly are you trying to accomplish? – gitaarik Jan 14 '14 at 00:12
  • You should quote the `?` in `file=...?...`. It's unlikely at best that it'll match anything, but technically it could, and with the right shell options it will cause an error if it doesn't – Kevin Jan 14 '14 at 00:34

1 Answers1

2

Don't think you can do it with just one bash expansion statement (unless they can be nested somehow), works fine with two though

$ start=${file%%\?*}; echo ${start##*.}
css
Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48
  • +1 - PEs can't be nested in the general case, and using two of them is exactly what we'd suggest if this question were asked in freenode's #bash. – Charles Duffy Jan 14 '14 at 01:00
  • @CharlesDuffy To clarify for anyone that tried nesting and succeeded (like I did), it's because you're using zsh which supports it (bash, ksh, etc don't). – mVChr Jan 14 '14 at 18:23