A variable assignment before a command sets an environment value for that command invocation without changing the whole script.

Program

Play the script to choose a log level and build the one-command environment prefix.

level
command_env.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

level="info"
command="LOG_LEVEL=$level ./run-report"
echo "$command"
#!/usr/bin/env bash

level="debug"
command="LOG_LEVEL=$level ./run-report"
echo "$command"
#!/usr/bin/env bash

level="warn"
command="LOG_LEVEL=$level ./run-report"
echo "$command"
  1. level ← info

    3level="info"4command="LOG_LEVEL=$level ./run-report"
    values this stepinfolevel
  2. command ← LOG_LEVEL=info ./run-report

    3level="info"4command="LOG_LEVEL=$level ./run-report"5echo "$command"
    values this stepLOG_LEVEL=info ./run-reportcommandinfolevel
  3. echo "$command"

    4command="LOG_LEVEL=$level ./run-report"5echo "$command"
    outputLOG_LEVEL=info ./run-report
    values this stepLOG_LEVEL=info ./run-reportcommand
  1. level ← debug

    3level="debug"4command="LOG_LEVEL=$level ./run-report"
    values this stepdebuglevel
  2. command ← LOG_LEVEL=debug ./run-report

    3level="debug"4command="LOG_LEVEL=$level ./run-report"5echo "$command"
    values this stepLOG_LEVEL=debug ./run-reportcommanddebuglevel
  3. echo "$command"

    4command="LOG_LEVEL=$level ./run-report"5echo "$command"
    outputLOG_LEVEL=debug ./run-report
    values this stepLOG_LEVEL=debug ./run-reportcommand
  1. level ← warn

    3level="warn"4command="LOG_LEVEL=$level ./run-report"
    values this stepwarnlevel
  2. command ← LOG_LEVEL=warn ./run-report

    3level="warn"4command="LOG_LEVEL=$level ./run-report"5echo "$command"
    values this stepLOG_LEVEL=warn ./run-reportcommandwarnlevel
  3. echo "$command"

    4command="LOG_LEVEL=$level ./run-report"5echo "$command"
    outputLOG_LEVEL=warn ./run-report
    values this stepLOG_LEVEL=warn ./run-reportcommand
environment prefix `NAME=value command` sets `NAME` only for that command run.
temporary value The prefix is useful when one command needs a setting but the script should not export it globally.
command plan This example prints command text instead of executing a project-specific program.