nixp.ru v3.0

16 апреля 2024,
вторник,
17:14:32 MSK

Genie написал 24 октября 2004 года в 15:08 (794 просмотра) Ведет себя как мужчина; открыл 40 тем в форуме, оставил 4758 комментариев на сайте.


$ var1=asjkhretioy; var2=var1; echo ${!var2}
asjkhretioy
Genie

man sh

${parameter}

The value of parameter is substituted. The braces are required when

parameter is a positional parameter with more than one digit, or when

parameter is followed by a character which is not to be interpreted as

part of its name.

If the first character of parameter is an exclamation point, a level of vari-

able indirection is introduced. Bash uses the value of the variable formed

from the rest of parameter as the name of the variable; this variable is then

expanded and that value is used in the rest of the substitution, rather than

the value of parameter itself. This is known as indirect expansion. The

exception to this is the expansion of ${!prefix*} described below.

Genie

кстати, это именно в bash работает.

а вот, к примеру, в ash — так не работает, ругается на «Bad substitution».

тогда это можно сделать так:

$ var1=asjkhretioy; var2=var1; eval "echo \$$var2"
asjkhretioy

Последние комментарии

ecobeingecobeing.ru
Экология и вегетарианство на благо всем живым существам Планеты.