0

Trying to assign value to a variable as follows

n=2
i='hello'
A$n='$i'

But getting error as "A2=hello: command not found

My aim is to assign 'hello' to A2 variable(by substituting $n as 2 in A$n)

3
  • With regards to your last line: single quoting will stop interpolation, meaning that A$n will be set to $i not the value of i. Commented Nov 7, 2022 at 14:53
  • After removing single quotes also getting same error. Tried like this A$n=$i Commented Nov 7, 2022 at 14:59
  • A$n='$i' simply is not a valid assignment statement, syntactically. Assignments are recognized by the parser before any parameter expansion occurs, and the portion before the = must be a valid identifier, which A$n is not. Commented Nov 7, 2022 at 15:02

1 Answer 1

1

You can use declare to achive this:

n=2
i='hello'
# as glenn mentioned it's safer to quote the declaration
# because the variables could possibly contain spaces 
declare "A$n=$i"
Sign up to request clarification or add additional context in comments.

1 Comment

Safer to write declare "A$n=$i", particularly if the values aren't known.

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.