Skip to contentSkip to conversation

Ignored alias inside for loop

DISCUSSION Rusty Bird 3 participants 5 messages Jul 26, 2015 – Jul 27, 2015

lore
Share & tools
lore

Rusty Bird

author
#0 lore raw

-----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-----

#1 lore raw
Quoted 11 lines · Rusty Bird

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.

Signature · 1 line
Jilles Tjoelker
author
#2 lore raw

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

Hi Jilles,

Quoted 3 lines

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.

Quoted 2 lines

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).

Quoted 5 lines

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:

Quoted 8 lines

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-----

#3 lore raw
Quoted 8 lines · Rusty Bird
Quoted 3 lines

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.

Quoted 5 lines
Quoted 2 lines

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.

Signature · 1 line
Jilles Tjoelker
#4 lore raw
In reply to Rusty Bird

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

Hello,

Quoted 5 lines
Quoted 2 lines

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.