Skip to main content
added 34 characters in body
Source Link
techraf
  • 6.1k
  • 11
  • 36
  • 51

The statement:

Because of that, any variables you create in the script aren't available to the subshell command.

is false. The scope of a variable defined in the parent shell is the entire script (including subshells created with command substitution).

Running:

#!/bin/bash
var=5.5555
ans1=$(echo $var)
ans2=$(var=6; echo $var)
echo $ans1
echo $ans2

will give the result:

5.5555
6

$var is resolved by the subshell:

  • if no local variable is specified, the value of three global variable is used
  • if a local variable is specified, it uses its value

See also the example 21-2.

The statement:

Because of that, any variables you create in the script aren't available to the subshell command.

is false. The scope of a variable defined in the parent shell is the entire script (including subshells).

Running:

#!/bin/bash
var=5.5555
ans1=$(echo $var)
ans2=$(var=6; echo $var)
echo $ans1
echo $ans2

will give the result:

5.5555
6

$var is resolved by the subshell:

  • if no local variable is specified, the value of three global variable is used
  • if a local variable is specified, it uses its value

See also the example 21-2.

The statement:

Because of that, any variables you create in the script aren't available to the subshell command.

is false. The scope of a variable defined in the parent shell is the entire script (including subshells created with command substitution).

Running:

#!/bin/bash
var=5.5555
ans1=$(echo $var)
ans2=$(var=6; echo $var)
echo $ans1
echo $ans2

will give the result:

5.5555
6

$var is resolved by the subshell:

  • if no local variable is specified, the value of three global variable is used
  • if a local variable is specified, it uses its value

See also the example 21-2.

added 159 characters in body; deleted 1 character in body
Source Link
techraf
  • 6.1k
  • 11
  • 36
  • 51

The statement:

Because of that, any variables you create in the script aren't available to the subshell command.

is false. The scope of a variable defined in the parent shell is the entire script (including subshells).

Running:

#!/bin/bash
var=5.5555
ans1=$(echo $var)
ans2=$(var=6; echo $var)
echo $ans1
echo $ans2

will give the result:

5.5555
6
  1. $var is resolved by subshell
  2. If no local value is specified, it uses the value from the parent script

$var is resolved by the subshell:

  • if no local variable is specified, the value of three global variable is used
  • if a local variable is specified, it uses its value

See also the example 21-2.

The statement:

Because of that, any variables you create in the script aren't available to the subshell command.

is false.

Running:

#!/bin/bash
var=5.5555
ans1=$(echo $var)
ans2=$(var=6; echo $var)
echo $ans1
echo $ans2

will give the result:

5.5555
6
  1. $var is resolved by subshell
  2. If no local value is specified, it uses the value from the parent script

See also the example 21-2.

The statement:

Because of that, any variables you create in the script aren't available to the subshell command.

is false. The scope of a variable defined in the parent shell is the entire script (including subshells).

Running:

#!/bin/bash
var=5.5555
ans1=$(echo $var)
ans2=$(var=6; echo $var)
echo $ans1
echo $ans2

will give the result:

5.5555
6

$var is resolved by the subshell:

  • if no local variable is specified, the value of three global variable is used
  • if a local variable is specified, it uses its value

See also the example 21-2.

deleted 112 characters in body; added 10 characters in body; added 75 characters in body
Source Link
techraf
  • 6.1k
  • 11
  • 36
  • 51

When bash executesThe statement:

Because of that, any variables you create in the script aren't available to the subshell command.

is false.

Running:

ans=$#!/bin/bash
var=5.5555
ans1=$(echo $var)
ans2=$(var=6; echo $var)
echo $ans1
echo $ans2

It will replace $var with its value in the parent shell, before executinggive the command. Subshell then gets a literal not a variable name to resolve.

Effectively you runresult:

ans=$(echo 5.5555)
6

You can run the script with bash -x to see what commands bash executes:

+ var=5.5555
++ echo 5.5555
+ ans=5.5555
+ echo 5.5555
5.5555
  1. $var is resolved by subshell
  2. If no local value is specified, it uses the value from the parent script

That's not a good example to compare withSee also the $BASH_SUBSHELL-behaviour from your commentexample 21-2.

When bash executes:

ans=$(echo $var)

It will replace $var with its value in the parent shell, before executing the command. Subshell then gets a literal not a variable name to resolve.

Effectively you run:

ans=$(echo 5.5555)

You can run the script with bash -x to see what commands bash executes:

+ var=5.5555
++ echo 5.5555
+ ans=5.5555
+ echo 5.5555
5.5555

That's not a good example to compare with $BASH_SUBSHELL-behaviour from your comment.

The statement:

Because of that, any variables you create in the script aren't available to the subshell command.

is false.

Running:

#!/bin/bash
var=5.5555
ans1=$(echo $var)
ans2=$(var=6; echo $var)
echo $ans1
echo $ans2

will give the result:

5.5555
6
  1. $var is resolved by subshell
  2. If no local value is specified, it uses the value from the parent script

See also the example 21-2.

added 82 characters in body; added 2 characters in body; added 12 characters in body; added 1 character in body; added 10 characters in body
Source Link
techraf
  • 6.1k
  • 11
  • 36
  • 51
Loading
added 83 characters in body; added 16 characters in body; edited body; added 5 characters in body
Source Link
techraf
  • 6.1k
  • 11
  • 36
  • 51
Loading
Source Link
techraf
  • 6.1k
  • 11
  • 36
  • 51
Loading