Skip to main content
Commonmark migration
Source Link

I am learning shell scripting through some online tutorials and I came to the following script that declares differences of global and local variables.

#!/bin/bash
# Experimenting with variable scope
var_change () {
  local var1='local 1'
  echo Inside function: var1 is $var1 : var2 is $var2
  var1='changed again'
  var2='changed again'
}

var1='global 1'
var2='global 2'

echo Before function call: var1 is $var1 : var2 is $var2

var_change

echo After function call: var1 is $var1 : var2 is $var2

And the output is:

Before function call: var1 is global 1 : var2 is global 2

 

Inside function: var1 is local 1 : var2 is global 2

 

After function call: var1 is global 1 : var2 is changed again

My Question...

Why is var1 after the function call "global 1" instead of "changed again"? Can someone explain?

I am learning shell scripting through some online tutorials and I came to the following script that declares differences of global and local variables.

#!/bin/bash
# Experimenting with variable scope
var_change () {
  local var1='local 1'
  echo Inside function: var1 is $var1 : var2 is $var2
  var1='changed again'
  var2='changed again'
}

var1='global 1'
var2='global 2'

echo Before function call: var1 is $var1 : var2 is $var2

var_change

echo After function call: var1 is $var1 : var2 is $var2

And the output is:

Before function call: var1 is global 1 : var2 is global 2

 

Inside function: var1 is local 1 : var2 is global 2

 

After function call: var1 is global 1 : var2 is changed again

My Question...

Why is var1 after the function call "global 1" instead of "changed again"? Can someone explain?

I am learning shell scripting through some online tutorials and I came to the following script that declares differences of global and local variables.

#!/bin/bash
# Experimenting with variable scope
var_change () {
  local var1='local 1'
  echo Inside function: var1 is $var1 : var2 is $var2
  var1='changed again'
  var2='changed again'
}

var1='global 1'
var2='global 2'

echo Before function call: var1 is $var1 : var2 is $var2

var_change

echo After function call: var1 is $var1 : var2 is $var2

And the output is:

Before function call: var1 is global 1 : var2 is global 2

Inside function: var1 is local 1 : var2 is global 2

After function call: var1 is global 1 : var2 is changed again

My Question...

Why is var1 after the function call "global 1" instead of "changed again"? Can someone explain?

deleted 23 characters in body
Source Link
Rui F Ribeiro
  • 58k
  • 28
  • 156
  • 237

I am a newbie learning shell scripting through some online tutorials and I came to the following script that declares differences of global and local variables.

#!/bin/bash
# Experimenting with variable scope
var_change () {
  local var1='local 1'
  echo Inside function: var1 is $var1 : var2 is $var2
  var1='changed again'
  var2='changed again'
}

var1='global 1'
var2='global 2'

echo Before function call: var1 is $var1 : var2 is $var2

var_change

echo After function call: var1 is $var1 : var2 is $var2

And the output is:

Before function call: var1 is global 1 : var2 is global 2

Inside function: var1 is local 1 : var2 is global 2

After function call: var1 is global 1 : var2 is changed again

My Question...

Why is var1 after the function call "global 1" instead of "changed again"? Can someone explain?

Thank you.

I am a newbie learning shell scripting through some online tutorials and I came to the following script that declares differences of global and local variables.

#!/bin/bash
# Experimenting with variable scope
var_change () {
  local var1='local 1'
  echo Inside function: var1 is $var1 : var2 is $var2
  var1='changed again'
  var2='changed again'
}

var1='global 1'
var2='global 2'

echo Before function call: var1 is $var1 : var2 is $var2

var_change

echo After function call: var1 is $var1 : var2 is $var2

And the output is:

Before function call: var1 is global 1 : var2 is global 2

Inside function: var1 is local 1 : var2 is global 2

After function call: var1 is global 1 : var2 is changed again

My Question...

Why is var1 after the function call "global 1" instead of "changed again"? Can someone explain?

Thank you.

I am learning shell scripting through some online tutorials and I came to the following script that declares differences of global and local variables.

#!/bin/bash
# Experimenting with variable scope
var_change () {
  local var1='local 1'
  echo Inside function: var1 is $var1 : var2 is $var2
  var1='changed again'
  var2='changed again'
}

var1='global 1'
var2='global 2'

echo Before function call: var1 is $var1 : var2 is $var2

var_change

echo After function call: var1 is $var1 : var2 is $var2

And the output is:

Before function call: var1 is global 1 : var2 is global 2

Inside function: var1 is local 1 : var2 is global 2

After function call: var1 is global 1 : var2 is changed again

My Question...

Why is var1 after the function call "global 1" instead of "changed again"? Can someone explain?

minor edit: removed '2 ' prefix in var2 assignment so it matches the description + wordsmithing
Source Link

I am a newbie learning shell scripting through some online tutorials and I came to the following script that declares differences of global and local variables.

#!/bin/bash
# Experimenting with variable scope
var_change () {
  local var1='local 1'
  echo Inside function: var1 is $var1 : var2 is $var2
  var1='changed again'
  var2='2 changedvar2='changed again'
}

var1='global 1'
var2='global 2'

echo Before function call: var1 is $var1 : var2 is $var2

var_change

echo After function call: var1 is $var1 : var2 is $var2

And the output is:

Before function call: var1 is global 1 : var2 is global 2

Inside function: var1 is local 1 : var2 is global 2

After function call: var1 is global 1 : var2 is changed again

My questionMy Question...

Why is why var1var1 after the function call is "global 1" instead of "changed again"? Can Can someone explain?

Thank you.

I am a newbie learning shell scripting through some online tutorials and I came to the following script that declares differences of global and local variables.

#!/bin/bash
# Experimenting with variable scope
var_change () {
  local var1='local 1'
  echo Inside function: var1 is $var1 : var2 is $var2
  var1='changed again'
  var2='2 changed again'
}

var1='global 1'
var2='global 2'

echo Before function call: var1 is $var1 : var2 is $var2

var_change

echo After function call: var1 is $var1 : var2 is $var2

And the output is:

Before function call: var1 is global 1 : var2 is global 2

Inside function: var1 is local 1 : var2 is global 2

After function call: var1 is global 1 : var2 is changed again

My question is why var1 after function call is "global 1" instead of "changed again"? Can someone explain?

Thank you.

I am a newbie learning shell scripting through some online tutorials and I came to the following script that declares differences of global and local variables.

#!/bin/bash
# Experimenting with variable scope
var_change () {
  local var1='local 1'
  echo Inside function: var1 is $var1 : var2 is $var2
  var1='changed again'
  var2='changed again'
}

var1='global 1'
var2='global 2'

echo Before function call: var1 is $var1 : var2 is $var2

var_change

echo After function call: var1 is $var1 : var2 is $var2

And the output is:

Before function call: var1 is global 1 : var2 is global 2

Inside function: var1 is local 1 : var2 is global 2

After function call: var1 is global 1 : var2 is changed again

My Question...

Why is var1 after the function call "global 1" instead of "changed again"? Can someone explain?

Thank you.

Source Link
hellojoshhhy
  • 245
  • 1
  • 3
  • 8
Loading