无法在 shell 脚本的 for 循环中分配变量

Cannot assign variable in for-loop in shell script

我试图在 for 循环中分配变量 a

#!/bin/bash

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

但是当我运行时,报错:

line 5: a: command not found

为什么会报错?如何赋值变量a?

在Shell.

中定义变量时,等号两边不能有空格

应该是:

a=$file

您需要删除空格,即

a = $file

必须

a=$file