zsh-workers
 help / color / mirror / code / Atom feed
From: "Øystein Walle" <oystwa@gmail.com>
To: zsh-workers@zsh.org
Subject: Read-only variables in emulation mode
Date: Mon, 16 Dec 2013 20:10:49 +0100	[thread overview]
Message-ID: <CAFaJEqs=P7yB-VGk=gJB2jzRf7zNiDkCuWWRADyY_xb52hGb_w@mail.gmail.com> (raw)

Hi,

Consider the following:

    emulate sh -c '
    some_name() {
        status=world
        echo "hello $status"
    }
    some_name
    '

Running it gives:

    some_name:1: read-only variable: status

This is a problem if you source a "library" meant to be used by
different versions of different shells:

    $ cat =git-example
    #!/bin/zsh
    emulate sh -c '
    . git-sh-setup
    '
    ...
    require_work_tree
    ...
    $ git example
    die_with_status:1: read-only variable: status

A common idiom in (ba)sh scripting is to use _ as a throwaway in a
while-read loop:

    while read -r col1 _ col2; do
        echo "col1 = $col1 and col2 = $col2"
    done < three-columns.txt

This also fails with an equivalent message when used in sh emulation.

Is it fixable? (Is the general opinion that it needs fixing?). I
assume status and _ are meant to be writable in POSIX shells.

I suppose an idea would be for various variables to be writable within
emulate -c '...' and later in the functions defined therein. How this
would affect the rest of the shell is hard to say for me.

In the mean time, is there any way around it?

Thanks
Øsse


             reply	other threads:[~2013-12-16 19:11 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-16 19:10 Øystein Walle [this message]
2013-12-16 19:40 ` Bart Schaefer
2013-12-18  6:36   ` Bart Schaefer
2013-12-18 19:28     ` Peter Stephenson
2013-12-19  7:57       ` Bart Schaefer
2015-06-16  0:10         ` Mikael Magnusson
2015-06-16  3:24           ` Bart Schaefer
2015-06-16  6:24             ` Mikael Magnusson
2015-06-16 15:54               ` Bart Schaefer
2015-06-16 21:06                 ` Mikael Magnusson
2015-06-16 19:05             ` Bart Schaefer

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='CAFaJEqs=P7yB-VGk=gJB2jzRf7zNiDkCuWWRADyY_xb52hGb_w@mail.gmail.com' \
    --to=oystwa@gmail.com \
    --cc=zsh-workers@zsh.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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