Echo output to terminal within function in BASH

后端 未结 4 1284
情深已故
情深已故 2020-12-31 04:38

I am writing a script in BASH. I have a function within the script that I want to provide progress feedback to the user. Only problem is that the echo command does not print

4条回答
  •  隐瞒了意图╮
    2020-12-31 05:33

    Dont use command substitution to obtain the return value from the function

    The return value is always available at the $? variable. You can use the variable rather than using command substitution

    Test

    $ function test_function {
    > return_val=10; 
    > echo "Echo value  to terminal $return_val";
    > return $return_val; 
    > }
    
    $ test_function
    Echo value  to terminal 10
    
    $ return_value=$?
    
    $ echo $return_value
    10
    

提交回复
热议问题