0

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.

6
  • Your question is utterly unclear. Commented Apr 7, 2014 at 7:00
  • String variables are in a text file, other than variable int. Can you clarify it. Commented Apr 7, 2014 at 7:00
  • edited, maybe now it is okay? sorry:) Commented Apr 7, 2014 at 7:01
  • 1
    like this echo ${!string_a}? Commented Apr 7, 2014 at 7:08
  • 1
    @user3162968 After foo=bar and bar=5, $foo produces "bar", while ${!foo} produces 5. The exclamation tells bash to use the value of foo as the name of the parameter to expand using $. It's called indirect parameter expansion. Commented Apr 7, 2014 at 14:41

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain me in a comment how I can write part of a single line in a code block? Four spaces does not work. I would like to write eval echo ${string} inside of a code block.
Use backticks. (`) - it's the last example in the first line (mini-Markdown) if you click help.
Never use eval when another technique will suffice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.