Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 14 Jun 2026 12:18:28 +0000
From:      bugzilla-noreply@freebsd.org
To:        bugs@FreeBSD.org
Subject:   [Bug 296050] sh.1: Description of 'local' is not very clear
Message-ID:  <bug-296050-227@https.bugs.freebsd.org/bugzilla/>

index | next in thread | raw e-mail

https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=296050

            Bug ID: 296050
           Summary: sh.1: Description of 'local' is not very clear
           Product: Documentation
           Version: Latest
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: Manual Pages
          Assignee: bugs@FreeBSD.org
          Reporter: temcbun@gmail.com
                CC: doc@FreeBSD.org

The sh(1) man page says:

"Variables may be declared to be local to a function by using the local
command.  This should appear as the first statement of a function, and the
syntax is: ..."

The part that I personally feel confused about is: "This should appear as the
first statement of a function" - it seems that in practice this is not the
case.  I created a test shell script to check this out:

-----------------------------------------------------
#!/bin/sh

a="Val 1"

foo()
{
        a="Val 3"
        echo "foo(): a before: ${a}"
        local a="Val 2"
        echo "foo(): a after: ${a}"
}

echo "a before: ${a}"
foo
echo "a after: ${a}"
-----------------------------------------------------

Here I intentionally made 'local' line not the first function statement, but
from the output of the script I can draw that the 'local' keyword still works
as it should, i.e. keeps a variable local to the function.  Output of the
script follows:

-----------------------------------------------------
a before: Val 1
foo(): a before: Val 3
foo(): a after: Val 2
a after: Val 3
-----------------------------------------------------

Please, let me know whether it is a bug in the manual page (the implementation
behaviour is correct, but the documentation is not), or it is a bug in the
implementation (the 'local' shouldn't work if it's not a first function
statement), or it is me who interpreted things the wrong way.

-- 
You are receiving this mail because:
You are the assignee for the bug.

home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bug-296050-227>