简体   繁体   English

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

[英]Cannot assign variable in for-loop in shell script

I tried to assign variable a in for-loop:我试图在for循环中分配变量a

#!/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 ?如何分配变量a

There can be no spaces on both sides of the equal sign when defining variables in the Shell.在 Shell 中定义变量时,等号两边不能有空格。

It should be:它应该是:

a=$file

You need remove spaces ie你需要删除空格,即

a = $file

must be一定是

a=$file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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