dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Ignored alias inside for loop
@ 2015-07-26  9:23 Rusty Bird
  2015-07-26 15:26 ` Jilles Tjoelker
  0 siblings, 1 reply; 5+ messages in thread
From: Rusty Bird @ 2015-07-26  9:23 UTC (permalink / raw)
  To: dash

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Hi,

Why does this ...

#!/bin/sh
for i in 1; do
    alias foobarbaz='echo ok'
    foobarbaz
done

... result in "foobarbaz: not found", but without the for loop it works?
Maybe I'm missing something in the spec, because bash-as-sh behaves
the same.

Rusty
-----BEGIN PGP SIGNATURE-----

iQJ8BAEBCgBmBQJVtKcoXxSAAAAAAC4AKGlzc3Vlci1mcHJAbm90YXRpb25zLm9w
ZW5wZ3AuZmlmdGhob3JzZW1hbi5uZXQ4NEI1OUJDRkM2MkIxMjlGRTFCMDZEMDQ0
NjlENzhGNDdBQUYyQURGAAoJEEadePR6ryrfWacQAI0/zf4f4K0oJ+xM0wprmtiq
4aj5UfOasXZji6cPvc0VGZcSK+rVFdT8EW6XIoMlkU8Lxp9aVLuaQtjcez9JhwIM
k0V7cNhAGuyIAPNL/t6CHPb92MXSfHXf0SFZ6oDilMMtc9i38GDvAeA1RuS0bhrl
W2kolIWeaaeQk2q5bMN66Mj86509NC9aXVBXatvCUHgqpNNDu2elgGOs/Isd2KVC
TDk4qqDgm5wTLuQ0DMZ6KF4Odut8pW7HDhJMO82Y2ghqp5WH3OTgcgR+XTXeN/9Y
Kp1Rix3Lse+7q6cMq7CdYhcGTh0eiDJxGaCNY7ho0klOH3MAvoQmEjJucXUpRB9T
wOCliQOtppyA9rvsDqLfHbPFp8EHYUaLc9vpNUatURa44GyU/ks2LPexrMDg9mgC
CmttZZVjDIe8L8mJ4Ot99dLmCsB2dIcJfM8lMTufFaWr5CrYNDCXx85dDHNYP416
3t4FHPh3WNk43KbAVfc4VvXn77Ao1HUVRhambxmux4B0fudqTFERtZv+u2EmHE5a
krp42fho4G65BC9Gewsy1lwUj3FRum5gXYzEoMV3gWFyLFVa1v+v8sC3DQbk8waq
cvv9XuUUTky1Ksudpo7WJboY50H36hlbdHVwrhQIX8SWN44IvAW6ceJ4r+VBBq+C
heQEjK/iUn0hkhtt8MdZ
=H2bf
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Ignored alias inside for loop
  2015-07-26  9:23 Ignored alias inside for loop Rusty Bird
@ 2015-07-26 15:26 ` Jilles Tjoelker
  2015-07-26 16:09   ` Rusty Bird
  0 siblings, 1 reply; 5+ messages in thread
From: Jilles Tjoelker @ 2015-07-26 15:26 UTC (permalink / raw)
  To: Rusty Bird; +Cc: dash

On Sun, Jul 26, 2015 at 09:23:52AM +0000, Rusty Bird wrote:
> Why does this ...

> #!/bin/sh
> for i in 1; do
>     alias foobarbaz='echo ok'
>     foobarbaz
> done

> ... result in "foobarbaz: not found", but without the for loop it works?
> Maybe I'm missing something in the spec, because bash-as-sh behaves
> the same.

Aliases are expanded during parsing, not during execution (like
functions are). The for loop is parsed completely before it is executed.

Using a function instead of an alias, or using eval will do what you
want.

Note that some parts of the parse/execute split are not specified by
POSIX. In particular, ksh93 and the real Bourne shell parse dot scripts
completely before executing anything, while most other shells parse as
needed. Example: given the file alias1.sh below:

alias foobarbaz='echo ok'
foobarbaz

The result is ok when running  dash -c '. ./alias1.sh'  but a command
not found error message when running  ksh93 -c '. ./alias1.sh'.

Similarly, a string as passed to the -c option or eval or trap builtins
may or may not be parsed fully before execution.

-- 
Jilles Tjoelker

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Ignored alias inside for loop
  2015-07-26 15:26 ` Jilles Tjoelker
@ 2015-07-26 16:09   ` Rusty Bird
  2015-07-26 18:41     ` Jilles Tjoelker
  2015-07-27  8:24     ` Seb
  0 siblings, 2 replies; 5+ messages in thread
From: Rusty Bird @ 2015-07-26 16:09 UTC (permalink / raw)
  To: dash; +Cc: Jilles Tjoelker

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Hi Jilles,

> Aliases are expanded during parsing, not during execution (like 
> functions are). The for loop is parsed completely before it is
> executed.

Ah, what confused me was that I wrongly remembered it working inside
an if (instead of for) block. But looking again now, only the alias
*definition* was inside.

> Using a function instead of an alias, or using eval will do what
> you want.

Do you mean replacing the line with: eval "alias foobarbaz='echo ok'"
That doesn't seem to work either (in dash or bash).

> Note that some parts of the parse/execute split are not specified
> by POSIX. In particular, ksh93 and the real Bourne shell parse dot
> scripts completely before executing anything, while most other
> shells parse as needed. Example: given the file alias1.sh below:
> 
> alias foobarbaz='echo ok' foobarbaz
> 
> The result is ok when running  dash -c '. ./alias1.sh'  but a
> command not found error message when running  ksh93 -c '.
> ./alias1.sh'.
> 
> Similarly, a string as passed to the -c option or eval or trap
> builtins may or may not be parsed fully before execution.

Interesting, thanks!

Rusty
-----BEGIN PGP SIGNATURE-----

iQJ8BAEBCgBmBQJVtQYoXxSAAAAAAC4AKGlzc3Vlci1mcHJAbm90YXRpb25zLm9w
ZW5wZ3AuZmlmdGhob3JzZW1hbi5uZXQ4NEI1OUJDRkM2MkIxMjlGRTFCMDZEMDQ0
NjlENzhGNDdBQUYyQURGAAoJEEadePR6ryrfCwUP/AtBWQpOEajCsghiYrkDmVpS
FuDMMo9s+uUTwJ6zrVviuj4cCfaigDBr1hlmc5ETjIMxbqgXYmG0o1GDkUvohKDR
4apjVRXATe8Xuia+E94rZTqv1iAv+udCD1BVhH9SB0Hbc9Jf9bxAqY6JI+GxoPRc
ea4tGDAPdPl9NJ69pPXP5AuDplqQOuap8BKzG00bu422z4CkIorvsg0W4+Ya+k+j
KZetjxj6N8vF17gOLpWNuxtJKbDEokNhHCGuf3hS1+7SEibJKYhb3GkyDGJ5EPYv
m3AocOlfSEuhZR8Dt5bK4gPNByrxl03PabBGmXVQaBzqjn+vIfykcuESXSHcl5SA
02FVY3pWAZKPCGWnG2JcGZpn6jQJQt5fNtM5ooixmzvFM/f0ItOL3EFKwHzIZxh1
ic+qIZcO8RrwfKktM3vytZ3fkSAVWJMcflj34+ce5A/LyfdrrqAkJGI3AbEul+Q6
mQoYZGBL7Uq2tgBWNveWzAByaewiW/+nlLhUXQh69L3X1cjkkrULGDTSaV+EYZ+z
mJP3qQcAB2DVnr757XgIh4kkDX8MDY4ZYVB+8ApgImUnBoklSSJeWXnBt+KwZgk7
Rok5FxIde3OgyYB9tjzDF/EAZXKTKNLrXFkhhat9j2c8+8VptVNcw77HP2bbcgOE
78H1zJW4UF8ntB5SGLTd
=qogf
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Ignored alias inside for loop
  2015-07-26 16:09   ` Rusty Bird
@ 2015-07-26 18:41     ` Jilles Tjoelker
  2015-07-27  8:24     ` Seb
  1 sibling, 0 replies; 5+ messages in thread
From: Jilles Tjoelker @ 2015-07-26 18:41 UTC (permalink / raw)
  To: Rusty Bird; +Cc: dash

On Sun, Jul 26, 2015 at 04:09:12PM +0000, Rusty Bird wrote:
> > Aliases are expanded during parsing, not during execution (like 
> > functions are). The for loop is parsed completely before it is
> > executed.

> Ah, what confused me was that I wrongly remembered it working inside
> an if (instead of for) block. But looking again now, only the alias
> *definition* was inside.

> > Using a function instead of an alias, or using eval will do what
> > you want.

> Do you mean replacing the line with: eval "alias foobarbaz='echo ok'"
> That doesn't seem to work either (in dash or bash).

eval foobarbaz

This may not be practical since it needs to be done for every alias use.

-- 
Jilles Tjoelker

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Ignored alias inside for loop
  2015-07-26 16:09   ` Rusty Bird
  2015-07-26 18:41     ` Jilles Tjoelker
@ 2015-07-27  8:24     ` Seb
  1 sibling, 0 replies; 5+ messages in thread
From: Seb @ 2015-07-27  8:24 UTC (permalink / raw)
  To: dash; +Cc: Rusty Bird

On Sun, Jul 26, 2015 at 04:09:12PM +0000, Rusty Bird wrote:

Hello,

> > Using a function instead of an alias, or using eval will do what
> > you want.
> 
> Do you mean replacing the line with: eval "alias foobarbaz='echo ok'"
> That doesn't seem to work either (in dash or bash).

In this case, where there is no further expansion to be done in the
alias content, you could use a mere variable:

  foobarbaz='echo ok'
  $foobarbaz "$@"

Or, if some further expansion has to be made, 'eval' again:

  foobarbaz='echo $PATH'
  eval $foobarbaz '"$@"'

++
Seb.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-07-27  8:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-26  9:23 Ignored alias inside for loop Rusty Bird
2015-07-26 15:26 ` Jilles Tjoelker
2015-07-26 16:09   ` Rusty Bird
2015-07-26 18:41     ` Jilles Tjoelker
2015-07-27  8:24     ` Seb

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).