init_hook completes.
Configuring scripts
Scripts can be added in yourdevbox.json. Scripts require a unique name, and a command or list of
commands to run:
Running your scripts
To run a script, usedevbox run <script_name>. This will start your shell, run your init_hook,
and then run the script:
Running a One-off Command
You can usedevbox run to run any command in your Devbox shell, even if you have not defined it as
a script. For example, you can run the command below to print “Hello World” in your Devbox shell:
devbox run, you should pass them before the command. For
example:
Run Scripts with Custom Environment Variables
You can use the--env flag to set custom environment variables in your Devbox shell. For example,
the following command will set the MY_VAR environment variable to my_value when running the
echo command:
--env-file flag. For example,
the following command will load the environment variables from the .env.devbox file in your
current directory:
Tips on using Scripts
- Since
init_hookruns every time you start your shell, you should primarily use it for setting environment variables and aliases. For longer running tasks like database setup, you can create and run a Devbox script - You can use Devbox scripts to start and manage long running background processes and daemons.
- For example — If you are working on a LAMP stack project, you can use scripts to start MySQL and Apache in separate shells and monitor their logs. Once you are done developing, you can use CTRL-C to exit the processes and shells
- If a script feels too long to put it directly in
devbox.json, you can save it as a shell script in your project, and then invoke it in yourdevbox scripts. - For more ideas, see the LAMP stack example in our Devbox examples repo.