Skip to main content
added an answer for the follow-up question
Source Link
Gilles 'SO- stop being evil'
  • 865.4k
  • 205
  • 1.8k
  • 2.3k

$(...) (aka "Command substitution") captures the output of the ... command. Assigning a value to a variable produces no output, so there's nothing to capture. In case #2, echo produces the output.

getStart () {
    local l=Hallo
    echo $l
}

v=$(getStart)
echo $v

To answer your update: the function outputs Hallo. This output is captured by the command substitution, because that's what command substitution does, so up to v=$(getStart), the script produces no output. Then the line echo $v outputs Hallo.

$(...) (aka "Command substitution") captures the output of the ... command. Assigning a value to a variable produces no output, so there's nothing to capture. In case #2, echo produces the output.

getStart () {
    local l=Hallo
    echo $l
}

v=$(getStart)
echo $v

$(...) (aka "Command substitution") captures the output of the ... command. Assigning a value to a variable produces no output, so there's nothing to capture. In case #2, echo produces the output.

getStart () {
    local l=Hallo
    echo $l
}

v=$(getStart)
echo $v

To answer your update: the function outputs Hallo. This output is captured by the command substitution, because that's what command substitution does, so up to v=$(getStart), the script produces no output. Then the line echo $v outputs Hallo.

added 110 characters in body
Source Link
choroba
  • 49.4k
  • 7
  • 92
  • 118

$(...) (aka "Command substitution") captures the output of the ... command. Assigning a value to a variable produces no output, so there's nothing to capture. In case #2, echo produces the output.

getStart () {
    local l=Hallo
    echo $l
}

v=$(getStart)
echo $v

$(...) (aka "Command substitution") captures the output of the ... command. Assigning a value to a variable produces no output, so there's nothing to capture. In case #2, echo produces the output.

$(...) (aka "Command substitution") captures the output of the ... command. Assigning a value to a variable produces no output, so there's nothing to capture. In case #2, echo produces the output.

getStart () {
    local l=Hallo
    echo $l
}

v=$(getStart)
echo $v
Source Link
choroba
  • 49.4k
  • 7
  • 92
  • 118

$(...) (aka "Command substitution") captures the output of the ... command. Assigning a value to a variable produces no output, so there's nothing to capture. In case #2, echo produces the output.