Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.shell > #26908

Re: Variable var names

From Christian Weisgerber <naddy@mips.inka.de>
Newsgroups comp.unix.shell
Subject Re: Variable var names
Date 2026-06-12 12:12 +0000
Message-ID <slrn112nttv.1fqc.naddy@lorvorc.mips.inka.de> (permalink)
References <n91qs8Fk65fU1@mid.individual.net>

Show all headers | View raw


On 2026-06-12, Frank Winkler <usenet@f.winkler-ka.de> wrote:

> $ n=1
> $ x1="  ok  "
> $ x2="  not ok  "
> $ eval echo \$x$n
> ok
> $ eval echo "\$x$n"
> ok
> $ n=2
> $ eval echo "\$x$n"
> not ok
> $
>
> So the variable var name seems to work but why are the blanks "deleted" 
> here?

The usual expansion, quote stripping, etc. happen, producing

  echo $x1
  echo $x2

which are then evaluated again.  You need to quote the quote
characters so they make it to the second round:

$ eval echo \"\$x$n\"
  not ok  

> Just out of curiosity, I tried this:
>
> $ IFS="" eval echo ".\$x$n."
> .  not ok  .
> $
>
> To my surprise, it works but why is IFS relevant here?

The result of substituting $x2 is subject to field splitting according
to the input field separators (IFS).

> And to my even bigger surprise, it looks like everything seems
> to behave as expected in the bash session but in the ksh script,
> IFS is not just changed for this single command but globally -

The latter is actually the POSIX-mandated behavior: "If the command
name is a special built-in utility, variable assignments shall
affect the current execution environment before the utility is
executed and remain in effect when the command completes".

Bash chooses to violate this, unless invoked with --posix or
POSIXLY_CORRECT.

The key term here is _special built-in_, as opposed to regular
built-ins.  The POSIX list of special built-ins is
  break, :, continue, ., eval, exec, exit, export, readonly,
  return, set, shift, times, trap, unset

-- 
Christian "naddy" Weisgerber                          naddy@mips.inka.de

Back to comp.unix.shell | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Variable var names Frank Winkler <usenet@f.winkler-ka.de> - 2026-06-12 09:30 +0200
  Re: Variable var names gazelle@shell.xmission.com (Kenny McCormack) - 2026-06-12 09:02 +0000
    Re: Variable var names Frank Winkler <usenet@f.winkler-ka.de> - 2026-06-13 12:29 +0200
      Re: Variable var names gazelle@shell.xmission.com (Kenny McCormack) - 2026-06-13 11:14 +0000
      Re: Variable var names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-06-13 13:15 +0200
  Re: Variable var names ram@zedat.fu-berlin.de (Stefan Ram) - 2026-06-12 11:26 +0000
    Re: Variable var names ram@zedat.fu-berlin.de (Stefan Ram) - 2026-06-12 11:38 +0000
      Re: Variable var names Frank Winkler <usenet@f.winkler-ka.de> - 2026-06-12 14:07 +0200
        Re: Variable var names ram@zedat.fu-berlin.de (Stefan Ram) - 2026-06-12 12:31 +0000
          Re: Variable var names Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-06-13 02:48 +0000
  Re: Variable var names Christian Weisgerber <naddy@mips.inka.de> - 2026-06-12 12:12 +0000
    Re: Variable var names Frank Winkler <usenet@f.winkler-ka.de> - 2026-06-12 15:19 +0200
    Re: Variable var names Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-06-13 02:51 +0000
  Re: Variable var names Geoff Clare <geoff@clare.See-My-Signature.invalid> - 2026-06-12 13:42 +0100
  Re: Variable var names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-06-12 17:41 +0200
    Re: Variable var names Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2026-06-12 17:54 +0200
    Re: Variable var names Frank Winkler <usenet@f.winkler-ka.de> - 2026-06-13 12:32 +0200
      Re: Variable var names Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-06-14 01:12 +0000
  Re: Variable var names Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-06-13 00:20 +0000
  Re: Variable var names Kaz Kylheku <046-301-5902@kylheku.com> - 2026-06-23 21:25 +0000
    Re: Variable var names Helmut Waitzmann <nn.throttle@erine.email> - 2026-06-24 10:45 +0200
    Re: Variable var names Christian Weisgerber <naddy@mips.inka.de> - 2026-06-24 11:41 +0000

csiph-web