Skip to main content
deleted 46 characters in body; edited title
Source Link
slm
  • 379.8k
  • 127
  • 793
  • 897

bash creating Creating n variables in Bash without assigning them one by one?

How do I create n variables in shell scripts without explicitly assigning them?

  What I mean is something like a loop that creates var1var1, var2var2, var3var3,...,varxvarx, where xx is a variable I set earlier, something like:

read x

for ((a=0;a<x;++a)); do
 variable$a=${RANDOM}
done

(Let's ignore the possibility that xx might be a string for now :D)

. But obviously, this doesn't work. How would one do this?

What I actually want to do, is that each argument I wrote in the command line when I executed the script to become it's own variable ARG1ARG1, ARG2ARG2... ARGnARGn with ${1}${1}, ${2}${2},..., ${n}${n} as it'sits value, so there will only be as many of these variables, as arguments were set.

Hope you guys understand what I mean :D Thanks.

bash creating n variables without assigning them one by one

How do I create n variables in shell scripts without explicitly assigning them?

  What I mean is something like a loop that creates var1, var2, var3,...,varx, where x is a variable I set earlier, something like:

read x

for ((a=0;a<x;++a)); do
 variable$a=${RANDOM}
done

(Let's ignore the possibility that x might be a string for now :D)

But obviously, this doesn't work. How would one do this?

What I actually want to do, is that each argument I wrote in the command line when I executed the script to become it's own variable ARG1, ARG2... ARGn with ${1}, ${2},..., ${n} as it's value, so there will only be as many of these variables, as arguments were set.

Hope you guys understand what I mean :D Thanks.

Creating n variables in Bash without assigning them one by one?

How do I create n variables in shell scripts without explicitly assigning them? What I mean is something like a loop that creates var1, var2, var3,...,varx, where x is a variable I set earlier, something like:

read x

for ((a=0;a<x;++a)); do
 variable$a=${RANDOM}
done

(Let's ignore the possibility that x might be a string for now. But obviously, this doesn't work. How would one do this?

What I actually want to do, is that each argument I wrote in the command line when I executed the script to become it's own variable ARG1, ARG2... ARGn with ${1}, ${2},..., ${n} as its value, so there will only be as many of these variables, as arguments were set.

Source Link
iamAguest
  • 553
  • 2
  • 8
  • 18

bash creating n variables without assigning them one by one

How do I create n variables in shell scripts without explicitly assigning them?

What I mean is something like a loop that creates var1, var2, var3,...,varx, where x is a variable I set earlier, something like:

read x

for ((a=0;a<x;++a)); do
 variable$a=${RANDOM}
done

(Let's ignore the possibility that x might be a string for now :D)

But obviously, this doesn't work. How would one do this?

What I actually want to do, is that each argument I wrote in the command line when I executed the script to become it's own variable ARG1, ARG2... ARGn with ${1}, ${2},..., ${n} as it's value, so there will only be as many of these variables, as arguments were set.

Hope you guys understand what I mean :D Thanks.