Skip to content

Instantly share code, notes, and snippets.

@ubuntupunk
Created April 8, 2026 09:42
Show Gist options
  • Select an option

  • Save ubuntupunk/77058c87c5e2a9cf7c47207e915ff1b8 to your computer and use it in GitHub Desktop.

Select an option

Save ubuntupunk/77058c87c5e2a9cf7c47207e915ff1b8 to your computer and use it in GitHub Desktop.
export $(cat .env.local | grep -v '^#' | xargs) && pnpm tsx scripts/seed-drizzle.ts
@ubuntupunk
Copy link
Copy Markdown
Author

Explanation of the Command

This is a single-line shell command (likely for Bash or a similar Unix-like shell) that sets up environment variables from a file and then runs a TypeScript script. It's commonly used in development workflows for Node.js projects (e.g., with tools like Next.js or pnpm) to seed a database using Drizzle ORM. I'll break it down step by step:

1. Overall Structure

The command combines two main actions with &&:

    First, it exports environment variables from a .env.local file.

    If that succeeds, it executes a script: pnpm tsx scripts/seed-drizzle.ts.

This ensures the script runs in an environment where the variables (e.g., database credentials) are available.

2. Breaking Down the First Part: export $(cat .env.local | grep -v '^#' | xargs)

cat .env.local: Reads the contents of the .env.local file (a common dotenv file for local environment variables, like DATABASE_URL=postgres://... or API_KEY=secret).

| grep -v '^#': Pipes the output to grep, which filters out lines starting with # (comments in env files). The -v flag inverts the match, so it keeps non-comment lines. This ignores any explanatory notes in the file.

| xargs: Pipes the filtered lines to xargs, which converts them into a single line of arguments (e.g., turns multi-line KEY1=value1 KEY2=value2 into KEY1=value1 KEY2=value2). It handles quoting and spacing automatically.

export $(...): Exports the resulting string as shell variables. The $(...) is command substitution—it runs the inner pipeline and inserts the output. export makes these variables available to child processes (like the upcoming script).

    Example: If .env.local contains:

    # Database config
    DATABASE_URL=postgres://localhost:5432/mydb
    NODE_ENV=development

     After filtering and xargs, it becomes something like export DATABASE_URL=postgres://localhost:5432/mydb NODE_ENV=development. This sets them as environment variables.

Note: This approach assumes simple key-value pairs without spaces or special characters in values. For complex env files, tools like dotenv (via npm) are safer, but this is a quick, lightweight alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment