Home > Blockchain >  Cannot assign variable in for-loop in shell script
Cannot assign variable in for-loop in shell script

Time:12-27

I tried to assign variable a in for-loop:

#!/bin/bash

for file in `ls`
do
  a = $file
done

But when I run, it gave error:

line 5: a: command not found

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

CodePudding user response:

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

It should be:

a=$file

CodePudding user response:

You need remove spaces i.e.

a = $file

must be

a=$file
  • Related