Document how to change the working directory in a recipe (#752)

This commit is contained in:
Casey Rodarmor 2021-02-02 23:47:07 -08:00 committed by GitHub
parent a14bc8c951
commit 6305114024
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -916,6 +916,38 @@ foo:
echo $x
```
=== Changing the Working Directory in a Recipe
Each recipe line is executed by a new shell, so if you change the working
directory on one line, it won't have an effect on later lines:
```make
foo:
pwd # This `pwd` will print the same directory…
cd bar
pwd # …as this `pwd`!
```
There are a couple ways around this. One is to call `cd` on the same line as
the command you want to run:
```make
foo:
cd bar && pwd
```
The other is to use a shebang recipe. Shebang recipe bodies are extracted and
run as scripts, so a single shell instance will run the whole thing, and thus a
`pwd` on one line will affect later lines, just like a shell script:
```make
foo:
#!/usr/bin/env bash
set -euxo pipefail
cd bar
pwd
```
=== Multi-line Constructs
Recipes without an initial shebang are evaluated and run line-by-line, which means that multi-line constructs probably won't do what you want.