1

I know that there are a lot of solutions on how to increment a variable inside a loop, but I'd like to know how to increment a variable which is a already a result from a command. Suppose I have files: test1 test2 test3 test4. My command is the following:

for i in {1..4}; do x$i=$(wc -l < test$i); done

I get:

x1=10: command not found

x2=9: command not found

x3=9: command not found

x4=9: command not found

which command could not be found? wc -l?

I guess the problem is related with the position of $symbol

1
  • 1
    The command not found is the one before the colon. So, x1=10, for example. This doesn't solve your problem, so this is just a comment for your future reference in command line or script debugging. Commented Sep 23, 2014 at 9:49

1 Answer 1

1

You can use declare built-in for this:

for i in {1..4}; do declare x$i=$(wc -l < test$i); done
Sign up to request clarification or add additional context in comments.

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.