in reply to Re^4: Indirect variable expansion
in thread Indirect variable expansion

Well, yes. At the time the variable substitution occurs, the variable has not been set yet. eg.:

$ quux=foo echo $quux $

"Fix" it by separating the one command into 2:

$ quux=foo; echo $quux foo $

It isn't just bash, BTW. Any Bourne-compatible shell should behave like this.


🦛

Replies are listed 'Best First'.
Re^6: Indirect variable expansion
by jeffenstein (Hermit) on Nov 18, 2024 at 16:10 UTC

    You would still have to export it to see it in the perl process:

    $ export M=MyMod $ perl -M$M -E 'say $ENV{M}->VERSION' $ unset M

    Or just do it in a subshell to keep your environment clean:

    $ (export M=MyMod; perl -M$M -E 'say $ENV{M}->VERSION')
      yes my point was to avoid the export, because it IS visible inside the Perl process

      ~$ M=whatever perl -E'say $ENV{M}' whatever

      unfortunately a dynamic use inside the process is not less complicated than the solutions already shown (eval or Ikegami's if 1 trick)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery