0

I'm trying to do a nested loop, one of which is indexing an array, but I also want to iterate through a custom list of strings, which happen to be only the letters C and I, nothing inbetween. My question is, what is the proper syntax for looping through this custom list of letters/strings? I saw some people use brackets{} to specify, what am I doing wrong?

abc=(thing1 thing2 thing3 thing4)

for j in C I; do
  for i in 0 1 2 3; do
    echo "grep 'ITEM_${abc[i]}' /folder1/folder2/THING_$jS_$i.sorted/585_10$jS_$i.sorted.gtf"
  done
done
3
  • 1
    And what exactly is the problem? I am guessing you don't want the echo there. Commented Aug 29, 2018 at 20:12
  • Problem is I want to iterate through two folder types and end with two output types, one with iterated C and separately, I (for each j) of the array Commented Aug 29, 2018 at 20:15
  • 3
    Showing your desired output would be a good place to start towards making this answerable. BTW, are you writing $jS_ when you want ${j}S_? Commented Aug 29, 2018 at 20:23

2 Answers 2

1
  • your quotes are wrong : Don't use single quotes around variables
  • separate variables from the surrounding text with ${var}, ex : /THING_${j}S_${i}.sorted

Learn how to quote properly in shell, it's very important :

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

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

3 Comments

We already have duplicate Q&A entries for both of the problems identified in this answer. If a question doesn't raise any new issues, it's typically appropriate to vote to close as duplicate (it's possible to have more than one duplicate linked to a single question, though it might need someone with enough rep to edit the dup list directly to enact that).
Thank you, however this is addressing the format of my code in terms of some sort of hierarchical guideline for readability, but doesn't actually address the core of my question.
Your code "fix" still doesn't answer the core of my question. It is iterating through a set of arbitrary letters that I don't want it to, but I'm going to fix it on my own with another array I guess. Could have used a simple reply like, no, you can't iterate through a custom set of loop arguments. Instead, "you're wrong learn how to do this properly". Thanks for your patronizing comment.
0

Since other users won't answer the question and instead want to brigade over proper quoting, which doesn't actually address what the original question is about, the answer is simply, you need to create a custom array that is composed of just C and I which you can call for each instance of said file you are trying to edit the name of.

Comments

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.