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
2
  • $i is expanded as part of assignment, use echo i am $i instead. Commented Sep 8, 2021 at 5: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. 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? Commented Sep 8, 2021 at 6:21

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

6 Comments

Can't you split the parts before and after that variable? part1 $variable part2 ??
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?
I updated my answer, You can replace your substr with the variable. @user1819769
Thank you once again. Then, how can we use test="select * from TABLE where A=$i and B="abcd"?
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
|
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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.