Error substituting bash for special variable

Asked 2 months ago, Updated 2 months ago, 2 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?

[[email protected]]#cat sample.sh
#!/bin/bash

echo$2

echo${2}

N = 2
echo${$N}

[[email protected]] #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


© 2022 OneMinuteCode. All rights reserved.