【问题标题】:creating associative array or hash table in bash [duplicate]在bash中创建关联数组或哈希表[重复]
【发布时间】:2015-12-11 02:02:28
【问题描述】:

我正在尝试在bash 中创建一个关联数组。我不确定它是否已创建,因为当我尝试打印它时,没有显示任何内容。

# create hash table of directories

START=0
END=39

# directory hash table
declare -A directories;

# directory name
dir_name="event-test-"

echo $dir_name

for i in {$START..$END}; do
        directories[$dir_name$i]=1;
        echo ${directories[@]};
done

当我source .sh 文件时得到的输出是:

event-test-
1

不确定我在尝试创建关联数组时是否遗漏了什么

【问题讨论】:

    标签: bash


    【解决方案1】:

    在 bash 的大括号扩展中不能使用变量。替换

    for i in {$START..$END};
    

    通过

    for ((i=$START;i<=$END;i++));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 2010-11-21
      • 2012-09-02
      相关资源
      最近更新 更多