# Coherence between inline and external script calling

**URL:** <https://cylc.discourse.group/t/coherence-between-inline-and-external-script-calling/883>\
**Category:** Cylc Support\
**Created:** [February 8, 2024, 6:54pm UTC](https://cylc.discourse.group/t/coherence-between-inline-and-external-script-calling/883 "2024-02-08T18:54:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abarral](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@abarral](https://cylc.discourse.group/u/abarral)\
**Post date:** [February 8, 2024, 6:54pm UTC](https://cylc.discourse.group/t/coherence-between-inline-and-external-script-calling/883/1 "2024-02-08T18:54:33Z")

</div>

From the [documentation](https://cylc.github.io/cylc-doc/latest/html/user-guide/task-implementation/job-scripts.html#jobscripts):

> The job script is separated into two parts. User scripts and environment (env-script, [environment], pre-script, script and post-script) are isolated and executed in a separate subshell process. Any changes to environment, traps, etc., done in any of them are visible in subsequent parts, but will not interfere with the parent shell process.

Now consider the following trivial workflow:

```cylc
[scheduling]
    [[graph]]
        R1 = "setup"

[runtime]
    [[setup]]
        pre-script="a=1"
        script="echo $a"

```

as expected, this works fine.  
However if we now create `bin/script.sh` containing “`a=1`” then

```cylc
[scheduling]
    [[graph]]
        R1 = "setup"

[runtime]
    [[setup]]
        pre-script=script.sh
        script="echo $a"

```

then the job errors, as `$a` isn’t defined.  
This can, of course, be fixed by changing `pre-script=script.sh` to `pre-script=". script.sh"`

* * *

This behavior is, as far as I’m aware, not documented (not even [here](https://cylc.github.io/cylc-doc/latest/html/user-guide/task-implementation/job-scripts.html#inlined-tasks)). It doesn’t feel intuitive, not feel in line with the documentation quoted above.

Is this the expected behavior ?

_(Using Cylc 8.2.4 with python3.9 on ubuntu 22)_

---

<div class="post-metadata">

**Author:** ![hilary.j.oliver](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/hilary.j.oliver/32/4_2.png) [@hilary.j.oliver](https://cylc.discourse.group/u/hilary.j.oliver)\
**Post date:** [February 8, 2024, 10:15pm UTC](https://cylc.discourse.group/t/coherence-between-inline-and-external-script-calling/883/2 "2024-02-08T22:15:37Z")

</div>

Hi @abarral

Technically that’s not a Cylc thing, it’s just down to the normal rules of shell variable scoping (well, and also the fact that Cylc simply writes `script` config items verbatim to the job file so it works just like any other script).

This:

```ini
# flow.cylc
[runtime]
   [[setup]]
        pre-script = "a=1"
        script = "script.sh" # contains "echo $a"

```

results in:

```auto
# job.sh
a=1
script.sh # runs in a subshell, so `a` is undefined unless you export it!

```

And, sourcing the script (e.g. `. ./script.sh`) works because that runs each line in the same shell as `a=1`, not a subshell.

Long story short, you just need:

```ini
pre-script = "export a=1"

```

---

<div class="post-metadata">

**Author:** ![abarral](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@abarral](https://cylc.discourse.group/u/abarral)\
**Post date:** [February 8, 2024, 10:24pm UTC](https://cylc.discourse.group/t/coherence-between-inline-and-external-script-calling/883/3 "2024-02-08T22:24:37Z")

</div>

> Cylc simply writes `script` config items verbatim to the job file

Thanks, that makes a lot of sense. That being said, I think it would be better if it were explicit in the documentation 🙂

---

<div class="post-metadata">

**Author:** ![hilary.j.oliver](https://yyz2.discourse-cdn.com/free1/user_avatar/cylc.discourse.group/hilary.j.oliver/32/4_2.png) [@hilary.j.oliver](https://cylc.discourse.group/u/hilary.j.oliver)\
**Post date:** [February 8, 2024, 10:26pm UTC](https://cylc.discourse.group/t/coherence-between-inline-and-external-script-calling/883/4 "2024-02-08T22:26:52Z")

</div>

Yes, I think we should document exactly how the `script` items relate to job file content.
