0

I am using global variable as INSTALL_DIR='/tmp' and then am calling function to use that variable in shell script

Which is the correct method from the following to use the variable?

Method 1:-

INSTALL_DIR='/tmp'

install_app() {

 echo "application path - $INSTALL_DIR"

}

install_app

Method 2:-

INSTALL_DIR='/tmp'

install_app() {
 app=$1
 echo "application path - $app"
}

install_app $INSTALL_DIR
Galet
  • 5,853
  • 21
  • 82
  • 148

1 Answers1

1

If you want to use variable in multiple places with in same script, then first approach is better.

But if variable will be used in different scripts , then you have to Export it (export will make it environment variable) in first script before using in second script. Refer to this link for exporting from one script to other.

Pass all variables from one shellscript to another?

For different scripts, use extra one-dot (.) to run . ./myscript.sh, it will execute within same scope.