Skip to main content
deleted 14 characters in body
Source Link
ЯegDwight
  • 25.3k
  • 10
  • 47
  • 52

declaredeclare inside a function doesn't work as expected. I needed read only-only global variables declared in a function. I tried this inside a function but it didn't work:

declare -r MY_VAR=1 

But this didn't work. Using readonly Using the readonly command worked for medid:

func() { readonly MY_VAR=1 } func echo $MY_VAR MY_VAR=2 

This will print 1 and gives and give the error MY_VAR"MY_VAR: readonly variablevariable" for the second assignment.

Regards, Kfir

declare inside a function doesn't work as expected. I needed read only global variables declared in a function. I tried this inside a function but it didn't work:

declare -r MY_VAR=1 

But this didn't work. Using readonly command worked for me:

func() { readonly MY_VAR=1 } func echo $MY_VAR MY_VAR=2 

will print 1 and gives the error MY_VAR: readonly variable for the second assignment.

Regards, Kfir

declare inside a function doesn't work as expected. I needed read-only global variables declared in a function. I tried this inside a function but it didn't work:

declare -r MY_VAR=1 

But this didn't work. Using the readonly command did:

func() { readonly MY_VAR=1 } func echo $MY_VAR MY_VAR=2 

This will print 1 and give the error "MY_VAR: readonly variable" for the second assignment.

Source Link

declare inside a function doesn't work as expected. I needed read only global variables declared in a function. I tried this inside a function but it didn't work:

declare -r MY_VAR=1 

But this didn't work. Using readonly command worked for me:

func() { readonly MY_VAR=1 } func echo $MY_VAR MY_VAR=2 

will print 1 and gives the error MY_VAR: readonly variable for the second assignment.

Regards, Kfir