Home > OS >  Using double variable in array declare - bad substitution
Using double variable in array declare - bad substitution

Time:01-21

I have been looking at other similar questions but didn't find the proper answer. Thanks for your help. $1 is array.

#!/bin/bash

for i in ${"$1"[@]}

do

echo "$i"

done

CodePudding user response:

If I don't misunderstand your not very well written question, you are looking for bash indirection, which is available since bash, version 2.

Below a minimal example

#! /bin/bash

foo=( 1 2 3 4 )
bar=( 5 6 7 8 )


if [[ ( "$1" -ne foo && "$1" -ne bar ) ]];
then 
    echo "Usage: $0 <foo|bar>"
    exit 1
fi

ambito="$1"[@]         # here what you are looking for

for i in ${!ambito};   # here the indirection
do
    echo -n "$i "
done
echo ""

And you can call this indirection.sh scrit as:

$ ./indirection.sh  foo
1 2 3 4 

or

$ ./indirection.sh  bar
5 6 7 8 

CodePudding user response:

$1 is the first command line argument, not all of them. Probably what you are looking for is $@.

And to create an array holding them, something like:

ambito=($@)
  •  Tags:  
  • Related