settings.sh assigns APP_PORT=8080.
Load it into the current shell so APP_PORT remains available after the command finishes.
Which POSIX-style command is appropriate?
. FILE, also written as source FILE in Bash, reads and executes the file in the current shell so assignments and functions remain available.
Detailed explanation
bash settings.shIncorrect. bash settings.sh runs a child Bash, so its variable changes do not return to the parent.
./settings.shIncorrect. Direct execution uses another process and does not change the parent shell's variables.
cat settings.shIncorrect. cat only prints the file and does not execute its assignments.
. ./settings.shCorrect. . ./settings.sh sources the file in the current shell.
Try it yourself
An example you can run in a temporary verification environment.
tmp=$(mktemp -d); printf 'APP_PORT=8080\n' >"$tmp/settings.sh"; . "$tmp/settings.sh"; printf '%s\n' "$APP_PORT"; rm -rf "$tmp"Expected result
8080Key points
- . is the source builtin
- It runs in the current shell
- It does not create a child shell
Notes
- Environment: Bash 5.x / 一時設定ファイル
- Use disposable shell sessions and harmless commands for experiments.
- Shell behavior can depend on Bash options and distribution defaults.
Foundation review
Place the operation
Sourcing is useful for configuration, but it executes shell code in the caller's context.
Check the side effect
Review the file contents and use a trusted path before sourcing it.