Search code examples
bashpass-by-value

bash - pass by value with no correct result


My code is the following:

vpnname="test vpn"
    
booleanCheck(){
    echo $1
    if (statement) then
       sendMail $1
    }
    
    sendMail(){
    mailsubject = "subject test for - $1" 
}
    
if ! ping 8.8.8.8 then
then 
    checkIfSendEmail "$@$vpnname"
fi

my value of the second last line passes greatly to booleanCheck() but once it gets to sendMail(), the value gets lost. what am i doing wrong?

i tried showing it by "$1" or '$1' or $@$1 o ${1} in the function sendMail() without a result


Solution

  • Wrap all your variables in double quotes while passing so any spaces in between won't get split:

    booleanCheck(){
        echo "$1"
        if (statement) then
            sendMail "$1"
        fi
    }
    

    Recommended: Use shellcheck to eliminate these obvious bad practices.