0

I have this script

$AIRLINECODE=xx
for i in $AIRLINECODE_response_*; do
  echo $i
done

prints all the files in the directory even if they have a completely different name..

what can i do to limit the for to find only the files that have the wished pattern?

1 Answer 1

2

This is because of how you are using the variable name.

When you use $AIRLINECODE_response_, bash interprets it as AIRLINECODE_response_ being the name of the variable. To make it work, use curly braces to specify what is the name: {AIRLINECODE}_response_.

All together:

AIRLINECODE=xx
for i in ${AIRLINECODE}_response_*; do
  echo "$i"
done

Also, note that you need to set the variable with AIRLINECODE=xx. It is wrong to use $AIRLINECODE=xx, because the variables are being set without the leading $.

Sign up to request clarification or add additional context in comments.

3 Comments

echo "$i" would be better a bit
I agree! And in general, I recommend it whenever I can, just now I forgot it :)
It would be even better to use find (and limit the search to the current directory) and xargs because if you have enough files in the directory ${AIRLINECODE}_response_* is going to fail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.