Why can't I use `declare -r` inside a function fo mark a variable readonly while `set -u` is in use?

James Caccese

With GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu),

#! /bin/bash
set -u

exec {FD1}>tmp1.txt
declare -r FD1
echo "fd1: $FD1"       # why does this work,

function f1() {
  exec {FD2}>tmp2.txt
  readonly FD2
  echo "fd2: $FD2"     # this work,
}

f1

function f2() {
  exec {FD3}>tmp3.txt
  echo "fd3: $FD3"     # and even this work,
  declare -r FD3
  echo "fd3: $FD3"     # when this complains: "FD3: unbound variable"?
}

f2

The goal is to make my file descriptor readonly

chepner

I don't think it's a bug. The exec statement assigns a value to the parameter FD3 in the global scope, while the declare statement creates a local parameter that shadows the global:

When used in a function, `declare' makes NAMEs local, as with the `local' command. The `-g' option suppresses this behavior.

It's the local parameter that is undefined. You can see this with a slightly different example:

$ FD3=foo
$ f () { FD3=bar; declare -r FD3=baz; echo $FD3; }
$ f
baz   # The value of the function-local parameter
$ echo $FD3
bar   # The value of the global parameter set in f

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

why i can't use javascript function created inside jquery object and how to declare custom function in jquery?

Why I can't use vars() inside my function for a variable that is outside my function?

Why can't I use this function with inside a function with const?

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

Why can't I use my variable inside this if statement?

Why can't I use an anonymous function inside a thread macro?

Why can't I use quotes inside of a function to

Can we use Conditional Compilation in VBA to declare variable inside a function?

Why can't I declare dynamic variable inside protocol

why can't I declare a global variable inside an "exec" string

How can I use a Declare variable inside OPENQUERY

Why I can't use argument for require function as a variable

Can't declare variable inside function on PostgreSQL

Why is this variable not global (can't use in a function)?

Why can I declare a variable without writing optional mark?

Why i can't use Variable in TRecord?

Why I can't use pointer to a function

Why can't I use a defined function inside another defined function,when creating an js object?

Why can't you declare a variable inside the expression portion of a do while loop?

How can I properly use While inside a Function?

Why can't I call map<U>(_ transform: (Wrapped) -> U) -> U? and use optional chaining at the same time?

Why can't I use variable inside @include in my Sass loop?

Why doesn't use the variable value inside the function in python

Why can't I use std::function as a std::set or std::unordered_set value type?

Why am I unable to declare a variable inside a function with a nested function?

Why use readonly IEnumerable<T>

Why can't I mock a function inside jest.Mock (have to use .mockImplementation on spyOn)?

Why can't I use an anonymous function inside jasmine's expectAsync?

Why can't I use the print function inside the class when doing operator overloading?

TOP Ranking

HotTag

Archive