1
updateEnvironmentField() {

          linewithoutquotes=`echo $LINE | tr -d '"'`
             b()
}

I want to pass variable named $linewithoutquotes to another method called b(), which is called from updateEnvironmentField() method. How to do the above requirement with shell script

Chai
  • 161
  • 1
    Is there any reason why just put $linewithoutquotes inside b()? b() will be invoked in a subshell and its environment will differ from others. – Tuyen Pham Jan 15 '19 at 02:43
  • 2
    A few options... make it a global variable, or pass in the value of your desired variable as an argument to the other function to name two of them. – Peschke Jan 15 '19 at 03:02

1 Answers1

2
b () {
    arg=$1
    # more code here
}

updateEnvironmentField () {
    linewithoutquotes=`echo $LINE | tr -d '"'`
    b "$linewithoutquotes"
}

Here, we call b with the the result of your command substitution as a string. The b function receives the string in the variable arg.

Note that you will want to use printf in place of echo and that you will need to properly quote the $LINE expansion so that whitespaces and globbing characters etc. are not messing up your data (I'm also changing the backticks to $(...) since that's more well behaved):

updateEnvironmentField () {
    linewithoutquotes=$( printf '%s\n' "$LINE" | tr -d '"' )
    b "$linewithoutquotes"
}

If you're using the bash shell, you may use its ${variable//pattern} parameter substitution to delete double quotes,

updateEnvironmentField () {
    linewithoutquotes=${LINE//\"}
    b "$linewithoutquotes"
}

or just

updateEnvironmentField () {
    b "${LINE//\"}"
}

Related:

Kusalananda
  • 333,661