Error substituting bash for special variable

Asked 1 years ago, Updated 1 years ago, 65 views

When you use bash to move sample.sh below, an error is generated with a special variable that substitutes the variable.
Probably because it is treated as a string, I don't think I can pull the arguments. Is there any way to do this?

[root@test]#cat sample.sh
#!/bin/bash

echo$2

echo${2}

N = 2
echo${$N}

[root@test] #sh sample.sh abc
B
B
sample.sh: line 8: ${$N}: incorrect substitution

bash shellscript

2022-09-30 11:44

1 Answers

#!/bin/bash

echo$2

echo${2}

N = 2
echo${!N}

so that

% ./sample.sh foo bar
bar
bar
bar

can be achieved.


2022-09-30 11:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.