Bash数组:意外的语法错误 [英] Bash array: Unexpected Syntax error

查看:125
本文介绍了Bash数组:意外的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照以下步骤编写这个简单的bash脚本.

I am writing this simple bash script as follows.

#!/bin/bash

array=( /home/abc/Downloads/something.bat /home/abc/Downloads/smb.conf )
echo ${array[@]}

我希望它能打印阵列中文件的所有名称.但我却收到此错误:

I expected it to print all the names of the files in the array. But I get this error instead:

test.sh: 3: Syntax error: "(" unexpected

如果我将数组的声明更改为

If I change the declaration of array to

array = {/home/abc/Downloads/something.bat /home/abc/Downloads/smb.conf}

此错误消失了,但我仍然有新错误

this error goes away but I still have new errors

test.sh: 3: array: not found
test.sh: 4: Bad substitution

如何解决此问题?这是我第一次使用Shell编程,所以我自己无法解决问题.

How can I resolve this issue? This is my first time in shell programming so I am unable to fix the issues myself.

已解决:

我以sh test.sh的身份执行它,但是我忘记了必须以 bash test.sh

I was executing it as sh test.sh but I forgot I had to execute it as bash test.sh

推荐答案

指针:空格对于BASH中的变量分配很重要.请勿在等号之前之前之后使用它们.

Pointer: Spaces are important with variable assignment in BASH. Don't use them before or after the equal sign.

这篇关于Bash数组:意外的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆