0

I have simple bash script. My bash version is GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-gnu)

#!/bin/bash
  
func() {
    nlvar="i'm not local variable"
    local lvar="i'm local variable"
}
echo $nlvar
echo $lvar

If I use

eval nlvar="i'm not local variable"

or

export nlvar="i'm not local variable"

still nothing is echoed

4
  • 2
    Is this the whole script? You defined the function. Where and how do you actually call it? Commented Dec 11, 2022 at 13:34
  • @KamilMaciorowski Yes, this is a whole script. I didn't call function. Just wanted to test the scope of variable. Commented Dec 11, 2022 at 15:40
  • If you don't call the function there is no scope except the global (undefined) one Commented Dec 11, 2022 at 15:51
  • @roaima Got it. Thank you! Commented Dec 11, 2022 at 16:09

1 Answer 1

1

If you don't call the function there is no scope except the global (undefined) one:

#!/bin/bash
  
func() { :; }    # define a function

echo $nlvar
echo $lvar

Defining a function does not execute it, so at the point of execution of the echo statements neither variable has been set

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.