• Uncategorized

About linux : Cannot-assign-variable-in-for-loop-in-shell-script

Question Detail

I tried to assign variable a in for-loop:


for file in `ls`
  a = $file

But when I run, it gave error:

line 5: a: command not found

Why did it give error? How to assign variable a?

Question Answer

There can be no spaces on both sides of the equal sign when defining variables in the Shell.

It should be:


You need remove spaces i.e.

a = $file

must be


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.