2 added 398 characters in body
source | link

Redirect the debugging output to standard error:

foo () {
   echo 'In foo' >&2
   VAR="1234"
   echo 'Calculated item' >&2
   echo "$VAR"
}

RESULT=$(foo)
printf 'RESULT="%s"\n' "$RESULT"

You could even create a dedicated "debug" function:

debug () {
    printf 'DEBUG: %s\n' "$@" >&2
}

foo () {
   debug 'In foo'
   VAR="1234"
   debug 'Calculated item'
   echo "$VAR"
}

RESULT=$(foo)
printf 'RESULT="%s"\n' "$RESULT"

Output:

$ sh script.sh
DEBUG: In foo
DEBUG: Calculated item
RESULT="1234"

Redirect the debugging output to standard error:

foo () {
   echo 'In foo' >&2
   VAR="1234"
   echo 'Calculated item' >&2
   echo "$VAR"
}

RESULT=$(foo)
printf 'RESULT="%s"\n' "$RESULT"

Redirect the debugging output to standard error:

foo () {
   echo 'In foo' >&2
   VAR="1234"
   echo 'Calculated item' >&2
   echo "$VAR"
}

RESULT=$(foo)
printf 'RESULT="%s"\n' "$RESULT"

You could even create a dedicated "debug" function:

debug () {
    printf 'DEBUG: %s\n' "$@" >&2
}

foo () {
   debug 'In foo'
   VAR="1234"
   debug 'Calculated item'
   echo "$VAR"
}

RESULT=$(foo)
printf 'RESULT="%s"\n' "$RESULT"

Output:

$ sh script.sh
DEBUG: In foo
DEBUG: Calculated item
RESULT="1234"
1
source | link

Redirect the debugging output to standard error:

foo () {
   echo 'In foo' >&2
   VAR="1234"
   echo 'Calculated item' >&2
   echo "$VAR"
}

RESULT=$(foo)
printf 'RESULT="%s"\n' "$RESULT"