dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jilles Tjoelker <jilles@stack.nl>
To: Rusty Bird <rustybird@openmailbox.org>
Cc: dash@vger.kernel.org
Subject: Re: Ignored alias inside for loop
Date: Sun, 26 Jul 2015 17:26:02 +0200	[thread overview]
Message-ID: <20150726152602.GA4346@stack.nl> (raw)
In-Reply-To: <55B4A728.9080303@openmailbox.org>

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

  reply	other threads:[~2015-07-26 15:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-26  9:23 Ignored alias inside for loop Rusty Bird
2015-07-26 15:26 ` Jilles Tjoelker [this message]
2015-07-26 16:09   ` Rusty Bird
2015-07-26 18:41     ` Jilles Tjoelker
2015-07-27  8:24     ` Seb

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150726152602.GA4346@stack.nl \
    --to=jilles@stack.nl \
    --cc=dash@vger.kernel.org \
    --cc=rustybird@openmailbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).