1

I can do this in Python:

def one(arg1):
    return arg1

def two(a,b):
    result=a+b
    return one(result)

two(1,3)

And it will work. But how do I do the same in a bash script?

1 Answer 1

3

Try that argument passing this way:

#!/usr/bin/env bash

function one(){
    # Print the result to stdout
    echo "$1"
}

function two() {
    local one=$1
    local two=$2
    # Do arithmetic and assign the result to
    # a variable named result
    result=$((one + two))
    # Pass the result of the arithmetic to
    # the function "one" above and catch it
    # in the variable $1
    one "$result"
}
# Call the function "two"
two 1 3
1
  • One thing that I've noticed when passing variables in functions across machines (like ssh) is the tendency for special characters like my_pwd='!Abc123' to get interpreted. A way to fix that is to declare it like so in the EOT setup --> my_pwd=\\$my_pwd Commented Apr 29, 2022 at 13:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .