简体   繁体   中英

Cannot assign variable in for-loop in shell script

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 ?

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

It should be:

a=$file

You need remove spaces ie

a = $file

must be

a=$file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM