问题描述:

I'm working to create a simple script where I can save each line as a variable for later use. However it didn't go well. Can anybody help me please.

read -p "Enter the number of user and press [ENTER]:" num

for ((i=1; i<=$num; i++)); do

var$i="|-> Line $i"

echo "var$i"

done

I'm trying to obtain the output that looks like this

|-> Line 1

|-> Line 2

|-> Line 3

|-> Line 4

网友答案:

You're not too far off. Just use an array:

read -p  "Enter the number of user and press [ENTER]:" num
for ((i=0; i<$num; i++)); do
    var[$i]="|-> Line $((i+1))"
    echo "${var[i]}"
done

for line in "${var[@]}"
do
    echo "Saved line: $line"
done

where the latter gives an example how to retrieve it later. Also, arrays are zero-indexed and I've adjusted it accordingly.

网友答案:

Can this meet your requirement?

read -p  "Enter the number of user and press [ENTER]:" num
   for ((i=1; i<=$num; i++)); do
       var_j="|-> Line $i"
       echo $var_j

   done
相关阅读:
Top