Re: Indirect expansion and arrays
Oops, sorry, that converts all of a to a scalar b so ${b[0]} gives "x y z" and ${b[1]} gives nothing. On Thu, Jul 29, 2010 at 7:16 PM, Dennis Williamson wrote: > To make your example work try: > > $ b=a[*] > > or > > $ b...@] > > Otherwise, your indirection is telling b to look at a as a scalar. > This would give the same result: > > $ echo $a > x > > On Thu, Jul 29, 2010 at 3:55 PM, Bernd Eggink wrote: >> It seems that indirect expansion doesn't work with arrays: >> >> $ a=(x y z) >> $ b=a >> $ echo "${!b[0]} ${!b[1]} ${!b[2]}" >> x >> >> Is that intended? The documentation isn't explicit about it. >> >> IMHO it would be very desirable to have a indirect expansion facility for >> arrays. Otherwise there is only a choice between passing all elements to a >> function, which is time-consuming, or using eval, which is cumbersome and >> error-prone. >> >> Regards, >> Bernd >> >> -- >> Bernd Eggink >> http://sudrala.de >> >> >
Re: Indirect expansion and arrays
To make your example work try: $ b=a[*] or $ b...@] Otherwise, your indirection is telling b to look at a as a scalar. This would give the same result: $ echo $a x On Thu, Jul 29, 2010 at 3:55 PM, Bernd Eggink wrote: > It seems that indirect expansion doesn't work with arrays: > > $ a=(x y z) > $ b=a > $ echo "${!b[0]} ${!b[1]} ${!b[2]}" > x > > Is that intended? The documentation isn't explicit about it. > > IMHO it would be very desirable to have a indirect expansion facility for > arrays. Otherwise there is only a choice between passing all elements to a > function, which is time-consuming, or using eval, which is cumbersome and > error-prone. > > Regards, > Bernd > > -- > Bernd Eggink > http://sudrala.de > >
Indirect expansion and arrays
It seems that indirect expansion doesn't work with arrays: $ a=(x y z) $ b=a $ echo "${!b[0]} ${!b[1]} ${!b[2]}" x Is that intended? The documentation isn't explicit about it. IMHO it would be very desirable to have a indirect expansion facility for arrays. Otherwise there is only a choice between passing all elements to a function, which is time-consuming, or using eval, which is cumbersome and error-prone. Regards, Bernd -- Bernd Eggink http://sudrala.de
Re: Bash interprets $! as an event
History expansion is performed before variable expansion. >From man bash: History expansion is performed immediately after a complete line is read, before the shell breaks it into words. and ! Start a history substitution, except when ***followed by a blank***, newline, carriage return, = or ( (when the extglob shell option is enabled using the shopt builtin). [emphasis mine] On Tue, Jul 27, 2010 at 1:01 PM, wrote: > Configuration Information [Automatically generated, do not change]: > Machine: x86_64 > OS: linux-gnu > Compiler: gcc > Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' > -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' > -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL > -DHAVE_CONFIG_H -I. -I../bash -I../bash/include -I../bash/lib -g -O2 > -Wall > uname output: Linux figment 2.6.32-24-server #38-Ubuntu SMP Mon Jul 5 > 10:29:32 UTC 2010 x86_64 GNU/Linux > Machine Type: x86_64-pc-linux-gnu > > Bash Version: 4.1 > Patch Level: 5 > Release Status: release > > Description: > > Event-catching seems to be catching cases it shouldn't. In particular, $! is > caught as an event. > > Repeat-By: > > This command fails as follows: > >> nicku...@woking:~$ sleep 5 & PID=$!; echo "pid is $PID" >> bash: !: event not found > > Adding quotation marks doesn't help: > >> nicku...@woking:~$ sleep 5 & PID="$!"; echo "pid is $PID" >> bash: !": event not found > > As a workaround, adding whitespace after the ! fixes it: > >> nicku...@woking:~$ sleep 5 & PID=$! ; echo "pid is $PID" >> [1] 1614 >> pid is 1614 > > or > >> nicku...@woking:~$ sleep 5 & PID="$! "; echo "pid is $PID" >> [3] 2048 >> pid is 2048 > >
Bash interprets $! as an event
Configuration Information [Automatically generated, do not change]: Machine: x86_64 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I../bash -I../bash/include -I../bash/lib -g -O2 -Wall uname output: Linux figment 2.6.32-24-server #38-Ubuntu SMP Mon Jul 5 10:29:32 UTC 2010 x86_64 GNU/Linux Machine Type: x86_64-pc-linux-gnu Bash Version: 4.1 Patch Level: 5 Release Status: release Description: Event-catching seems to be catching cases it shouldn't. In particular, $! is caught as an event. Repeat-By: This command fails as follows: > nicku...@woking:~$ sleep 5 & PID=$!; echo "pid is $PID" > bash: !: event not found Adding quotation marks doesn't help: > nicku...@woking:~$ sleep 5 & PID="$!"; echo "pid is $PID" > bash: !": event not found As a workaround, adding whitespace after the ! fixes it: > nicku...@woking:~$ sleep 5 & PID=$! ; echo "pid is $PID" > [1] 1614 > pid is 1614 or > nicku...@woking:~$ sleep 5 & PID="$! "; echo "pid is $PID" > [3] 2048 > pid is 2048