2
schema_dir=schema_app
for SCHEMA_CHANGE in $schema_dir/*
do

so my pwd is /A/B/C/scripts within which I have subdirectories (schema_app, schema_data, etc). I want a for loop to run through the files in each directory, depending upon the parameter I pass to the script initially.

What mistake am I doing here ? Because, when I echo SCHEMA_CHANGE (the file name), it is showing like this schema_app/filename. how can I get only the filename here ?

  • printf %s\\n "${SCHEMA_CHANGE#*_*/}" – mikeserv Dec 13 '15 at 22:25
2

You aren't doing anything wrong. schema_app/filename is the correct path to the file. It's what you need to pass to a command (or a redirection) to work on the file. Using filename alone wouldn't work: how would the command know in what directory the file is?

If for some reason you need the filename without its directory part, you can use

"${SCHEMA_CHANGE##*/}"

This takes the value of the variable SCHEMA_CHANGE and removes the part up to and including the last / (or leaves the value unchanged if there's no /), so you get the filename without its directory part.

  • Thanks Gilles. This works as expected... Curious what does the '##*/' mean though ? – MRKR Dec 14 '15 at 14:46
  • @MRKR takes the value of the variable SCHEMA_CHANGE and removes the part up to and including the last / (removes the longest prefix matching */). – Gilles Dec 14 '15 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.