user3162968
user3162968

Reputation: 1046

Bash, integer variable inside of a string variable

Greetins, I need to do the following:

string_a="string_a${int}"
string_b="string_b${int}"
int=1

String variables are in a text file, other than file which contains bash script and where variable int is declared. I need that I write string_a${int} in a text file, and sed that line from this file so that it becomes a string variable with nested int variable ${int}. Do you know any nice solution for that? :)

Maybe I try to write codes in both files:

File "string"

aaaaaaaaa${i}

File "program"

#!/bin/bash

int=1
i=1

string=$(sed -n "${int}p" string)

echo ${string}

As you can see, I try to sed first line from file "string", but as a result I get aaaaaaaaa${i} instead of aaaaaaaaa1.

Upvotes: 0

Views: 531

Answers (1)

user3162968
user3162968

Reputation: 1046

I've got a solution. I need to write eval echo ${string} instead of echo ${string}. I hope that it helps someone.

Upvotes: 1

Related Questions