0

In order to obtain the results of consecutive numbers, a bash script was created as follows. However, the results did not come out as intended.

#!/bin/bash

test="i am $i"

for i in {1..10}
do
        echo "$test"
done

result

sh test.sh

i am 
i am 
i am 
i am 
i am 
i am 
i am 
i am 
i am 
i am 

But the result I want is... As shown below, how do we deal with the variables to get the results?

i am 1
i am 2
i am 3 
i am 4
i am 5
i am 6
i am 7
i am 8
i am 9
i am 10
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • `$i` is expanded as part of assignment, use `echo i am $i` instead. – oguz ismail Sep 08 '21 at 05:27
  • 1
    Variables contain static strings, not dynamic instructions for generating strings, so fundamentally this won't work. But this seems like an [XY problem](https://xyproblem.info). Depending on what the bigger picture is (what the larger problem you're trying to solve is), a function might be a good solution, or maybe a `printf` format string, or... I dunno. What's the actual goal here? – Gordon Davisson Sep 08 '21 at 06:21

3 Answers3

1

Use $i outside of the variable

#!/bin/bash

test="i am "

for i in {1..10}
do
  echo $test $i
done

Also you can use ${i} inside of the variable

#!/bin/bash

for i in {1..10}
do
  test="i am ${i}"
  echo $test
done

The result is:

i am  1
i am  2
i am  3
i am  4
i am  5
i am  6
i am  7
i am  8
i am  9
i am  10

Or you can replace substr with anything you want inside.

For example

#!/bin/bash
test="I am SUBSTR"

for i in {1..10}
do
  echo ${test/SUBSTR/$i}
done

When you have multiple variables, I know this solution:

#!/bin/bash
test="I am SUBSTR and STR2"

for i in {1..10}
do
  o=${test/SUBSTR/$i}
  echo ${o/STR2/$i*$i}
done

Using sed also can help

#!/bin/bash
test="I am SUBSTR and STR2"

for i in {1..10}
do
  echo $test | sed  -e 's/SUBSTR/'$i'/;s/STR2/'$i++'/'
done
Saeed
  • 5,413
  • 3
  • 26
  • 40
  • Can't you split the parts before and after that variable? `part1 $variable part2` ?? – Saeed Sep 08 '21 at 05:47
  • Thank you. You should use $i in the middle of the sentence. We want to use $i as the where condition for the DB query. Is there any way to declare and use the $test externally? – user1819769 Sep 08 '21 at 05:52
  • I updated my answer, You can replace your substr with the variable. @user1819769 – Saeed Sep 08 '21 at 05:56
  • Thank you once again. Then, how can we use test="select * from TABLE where A=$i and B="abcd"? – user1819769 Sep 08 '21 at 06:12
  • 1
    define test like `test="select * from TABLE where A=COUNTER and B="abcd"` and in loop replace COUNTER with `$i`. Did you try this? @user1819769 – Saeed Sep 08 '21 at 06:14
  • I add a solution with 2 variables inside string, I know it is messy but I hope it helps. search more about replacing in bash. @user1819769 – Saeed Sep 08 '21 at 06:25
0

You need to write a function in order to delay the evaluation.

#!/bin/bash

message () { echo "i am $1"; }

for i in {1..10}
do
  message $i
done

Or the following, if you just want to craft the message.

#!/bin/bash

message () { echo "i am $1"; }

for i in {1..10}
do
  test="$(message $i)"
  echo "$test"
done
ceving
  • 21,900
  • 13
  • 104
  • 178
0
#!/bin/bash

test='i am $i'

for i in {1..10}
do
  eval echo "$test"
done

or

#!/bin/bash

test='echo "i am $i"'

for i in {1..10}
do
  eval "$test"
done

Example:

yanyong@master:~$ test='echo "i am $i"'; for i in {1..10}; do eval "$test"; done
i am 1
i am 2
i am 3
i am 4
i am 5
i am 6
i am 7
i am 8
i am 9
i am 10

References:

https://man7.org/linux/man-pages/man1/eval.1p.html

yanyong
  • 1
  • 2