Using local parameter with the same name of a special one within a function

Damien Flament

As I like to use descriptive parameter names, I often tried to use variable names already used by Zsh itself.

As it took me into trouble in the past, I now wrap my script in a main function named after the script. Then I declare all my local parameter using the local keyword.

But some parameters seems to not allow hiding them in the local scope:

function foo
{
    local commands=(bar baz)
    local status=0

    echo ${(F)commands}
    echo $status
}

Here, the local commands parameter is used in place of the special one.

But as the status parameter is read-only, I got the following error:

zdm:6: read-only variable: status

I think that the local keyword allow to use a special parameter in a local scope but it does not change the way it was declared.

Damien Flament

To use a special parameter name within a local scope, you have to hide it explicitly using the -h flag of the typeset builtin:

typeset -h status

As you want to declare it as local, use the local keyword which also supports that flag:

local -h status

You can also use the private keyword from the zsh/param/private module:

zmodload zsh/param/private

private -h status

The -h flag

From the Zsh manual:

-h

Hide: only useful for special parameters (those marked <S> in the table in Parameters Set By The Shell), and for local parameters with the same name as a special parameter, though harmless for others.

A special parameter with this attribute will not retain its special effect when made local. Thus after typeset -h PATH, a function containing typeset PATH will create an ordinary local parameter without the usual behaviour of PATH.

Alternatively, the local parameter may itself be given this attribute; hence inside a function typeset -h PATH creates an ordinary local parameter and the special PATH parameter is not altered in any way.

It is also possible to create a local parameter using typeset +h special, where the local copy of special will retain its special properties regardless of having the -h attribute.

Global special parameters loaded from shell modules (currently those in zsh/mapfile and zsh/parameter) are automatically given the -h attribute to avoid name clashes.

Local parameters

For a more understandable explanation, see the Zsh manual page about the local parameters.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can't use let to declare a variable using the same name as one function parameter, even the parameter get set default value?

Javascript closure (using parameter name to define an object in the same function)

The local var does not override the parameter of the same name?

Referencing local variable within the same function

What does "local variables at the outermost scope of the function may not use the same name as any parameter" mean?

Is it a bad practice to have local variable name same as parameter name in c?

Can a function and local variable have the same name?

Same function name different parameter type

Getting Class Name in TypeScript inside an export function from a special parameter

passing code to a function as parameter within "using statement"

Using local variables in one function in a main function

how to assign parameter passed to constructor to local private variable with the same name

Why can't a default parameter and function parameter have the same name?

User defined function with the same syntax as strlen() using only one variable other than the formal parameter?

Overview (List) off Organizations with the same Domain Name within one subscription

How to take the name of a parameter variable as a string (within a function) in python

calling a global function from a local function with the same name in swift

How to call a function recursively if it contains a local function with the same name?

Using Local Special Variables

Powershell [ValidateSet()] Between Separate Parameter Sets Using Same Parameter Name

How to call a function with the same name within another class?

Set global variable from within function using function parameter with JavaScript

How to checkout a local branch from a remote's one with the same name?

PowerShell function ValueFromPipelineByPropertyName not using alias parameter name

Pass name of function to execute in parameter using jQuery

Using function parameter names that are the same as passed variables

Using a class object as a parameter within the same class object

using local variable from one function to another

Filter function will only work using one parameter

TOP Ranking

HotTag

Archive