0

I am trying to use a Shell Variable inside a shell script , My shell script is as below

HerculesResponse=$(curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{ "testID": "591dc3cc4d5c8100054cc30b", "testName": "stagetest", "poolID": "5818baa1e4b0c84637ce36b4", "poolName": "Default", "dashboardID": "582e3a2ff5c650000124c18a", "dashboardName": "Default", "dateCreated": "2017-05-23T13:51:23.558Z", "callbackHeader": {}, "active": true }' "https://example.com:8080/run") reportURL=$(expr "$HerculesResponse" : '.*"reportURL":"\([^"]*\)"') echo $reportURL runId=$(echo $reportURL | cut -d"=" -f 2) echo $runId

How do I use runId variable outside of this shell script to run the command

testStatus=$(curl -X GET https://example.com:8080/runs/$runId)

I tried to use export runId command but doesn't work

codeforester
  • 39,467
  • 16
  • 112
  • 140
Siraj Syed
  • 1,151
  • 1
  • 11
  • 17
  • `export` makes variables available to *child* processes -- that is, processes you later start. It doesn't make them available to *parent* processes (ie. the process that started you). – Charles Duffy May 24 '17 at 00:52

1 Answers1

1

When you run your shell script, the variables set by it are lost once its execution finishes and they won't be available to the calling shell. The right way to extract the value of your variable is to:

  • let the script output the value of the variable and use command substitution to assign that value to a variable in the calling shell, like this:

    run_id=$(/path/to/script.sh)
    

The drawback of this approach is that all output of the script will end up in the variable. In your case, the output of echo $reportURL as well as echo $runId.

  • run the script in current shell with . or source command, like this:

    . /path/to/script.sh
    

    or

    source /path/to/script.sh
    

See also:

codeforester
  • 39,467
  • 16
  • 112
  • 140