for (( a=1;a<=$(wc -l sedtest1);a++ ));do echo $a; done
Gives me an error:
-bash: ((: a<=21 sedtest1: syntax error in expression (error token is "sedtest1")
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
The output of
wc -l sedtest1 will be something like:
So the test will become something like
a <= 21 sedtest1 which is invalid.
Also, that does mean that the
wc command will be run for each iteration of the loop. If the content of the
sedtest1 file doesn't change between each iteration, it would be better to save that number of line first in a variable outside the loop:
n=$(wc -l < sedtest1) # using redirection avoids the output containing the filename for ((a = 0; a < n; a++)); do echo "$a"; done
I also suspect that you're trying to use a loop to process text in an inefficient and non-shell way. You may want to read Why is using a shell loop to process text considered bad practice?. Looping over each line of a file in that way is not the right way to go.