Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 8803  / 1 Year ago, wed, january 11, 2023, 11:21:32

I am trying to write a bash script to print values of the 3 variables. Keep getting errors. What am I doing wrong?



INPUT1=/tmp/dir1
INPUT2=/tmp/dir2
INPUT3=/tmp/dir3

for i in 1 2 3
do
echo $(INPUT$i)
done


When I run this script, tho output is:



syntax error: operand expected (error token is "/tmp/dir1

More From » bash

 Answers
0

Bash doesn't support that kind of syntax directly. You could use 'eval', however with modern versions of 'bash' the cleanest way imho is via an array



input=( "/tmp/dir1" "/tmp/dir2" "/tmp/dir3" )

for i in {0,1,2}; do echo "${input[i]}"; done


(note that bash arrays are zero-indexed); or to list all elements you can use



for i in "${input[@]}"; do echo "$i"; done

[#29771] Wednesday, January 11, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
motivengry

Total Points: 459
Total Questions: 112
Total Answers: 108

Location: Bonaire
Member since Sat, May 1, 2021
3 Years ago
motivengry questions
Tue, Oct 4, 22, 10:02, 2 Years ago
Wed, May 31, 23, 14:33, 12 Months ago
;