Skip to content

Bash array dynamic name

wordpress meta

title: 'Bash Array Dynamic Name'
date: '2019-06-14T15:19:26-05:00'
status: publish
permalink: /bash-array-dynamic-name
author: admin
excerpt: ''
type: post
id: 1358
    - Uncategorized
tag: []
post_format: []

Sometimes you want to have dynamic array names to simplify code. Below is one way of making the array name dynamic in a loop.

``` #!/bin/bash section1=( fs-01 fs-02 ) section2=( fs-03 ) function snap() { tag=$1 echo echo "TAG: $tag" x=$tag var=$x[@] for f in "${!var}" do echo "fss: $f" done } snap "section1" snap "section2"
</div>And output like this.

<div class="wp-block-syntaxhighlighter-code ">```

# ./

TAG: section1
fss: fs-01
fss: fs-02

TAG: section2
fss: fs-03