1

I'm using the following command to try to max out the number of cores on my server:

for i in {1..`nproc --all`}; do while : ; do : ; done & done

For some reason, it only ever uses one core, even though my server has two. If I try to replace `nproc --all` with 2, it works fine. Why is this happening?

2

Brace expansion happens before command substitution. This fragment

{1..`nproc --all`}

will not be expanded as brace at all. When the command substitution is expanded to 2, it becomes

{1..2}

but it's too late for brace expansion now. The same problem would be if you used a variable there.


This syntax works:

for ((i=1; i<=`nproc --all`; i++)); do echo "$i"; done

or

for i in $(seq 1 "$(nproc --all)"); do echo "$i"; done

Adjust the loop to your needs. Note in the last case I used $(…) instead of `…` because this form nests easily.

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.