---
title: Dynamically run build commands
description: Learn how to run different scripts based on the environment or branch.
url: /kb/guide/dynamic-build-commands
canonical_url: "https://vercel.com/kb/guide/dynamic-build-commands"
last_updated: 2026-07-16
authors: DX Team
related:
  - /docs/deployments/configure-a-build
  - /docs/environment-variables/system-environment-variables
install_vercel_plugin: npx plugins add vercel/vercel-plugin
---

In this example, you'll create a `vercel.sh` file in your repository that your project's [Build Command](/docs/deployments/configure-a-build#build-command) can use to run conditional logic..

1. Set up your scripts in `package.json`. Specify the following example scripts to be run for your preview and production environments:
   
   `{ "scripts": { "build:production": "next build", "build:preview": "echo \"Let's build a preview!\" && next build" } }`
   
2. Create a `vercel.sh` file in your project's root directory. Use the following conditional logic to run a script according to the environment:
   
   `#!/bin/bash if [[ $VERCEL_ENV == "production" ]] ; then npm run build:production else npm run build:preview fi`        You can adjust this to use any other [System Environment Variables](/docs/environment-variables/system-environment-variables) on Vercel. For example, you can use `VERCEL_BRANCH_URL` to run a script according to the branch name.
   
3. Set your Build Command to use `vercel.sh` either in your `vercel.json` file or in the project dashboard:
   

- **Using a** `**vercel.json**` **file**:
  
  `{ "$schema": "https://openapi.vercel.sh/vercel.json", "buildCommand": "sh vercel.sh" }`
  
- **Using the project dashboard**:
  
  Select your project from the [dashboard](/dashboard) and go to the **Settings** tab. Under **Build & Development Settings**, set the value of the [Build Command](/docs/deployments/configure-a-build#build-command) to be `sh vercel.sh`. This runs the `vercel.sh` script created earlier.