kasper
kasper

Reputation: 631

setting variable with unknown name in bash

I would like to set a variable which name is stored in a file (is an output of a sed executed earlier)

the file would look like:
py1
so setting our variable would be like: set cat file=value
but echoing $py1 gives me nothing.

Is that possible with bash version 2.05?

Upvotes: 2

Views: 1379

Answers (3)

Eugene Yarmash
Eugene Yarmash

Reputation: 149756

Use eval:

eval "$(cat file)=value"

Update: The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

Upvotes: 2

kurumi
kurumi

Reputation: 25599

This is the "preferred" way to do it (bash) without using cat or eval

declare $(<file)=value

Upvotes: 3

Paul Creasey
Paul Creasey

Reputation: 28824

You'll need to use eval

eval $(cat file)=value

Upvotes: 0

Related Questions