# command

## Usage

```bash
bdy target exec command|cmd [options] <identifier> [command]
```

## Arguments

| **Argument** | **Description** |
| :--- | :--- |
| `identifier` | Target identifier (required) |
| `command` | Command to execute on the target (e.g. a SQL query or a shell command) (required) |

## Options

| **Option** | **Description** |
| :--- | :--- |
| `-w, --workspace <domain>` | Workspace URL handle. Can be set via variable: BUDDY_WORKSPACE |
| `-p, --project <name>` | Project name. Can be set via variable: BUDDY_PROJECT |
| `--pipeline <identifier>` | Pipeline identifier to scope the target to. Requires --project |
| `--environment <identifier>` | Environment identifier to scope the target to. Resolves a workspace environment; add --project to point at a project environment |
| `--async` | Start the command and return the execution id without waiting for completion |
| `--file <path>` | Read the command from a file instead of the argument |
| `--format <text|json>` | Output format: json, text. Default: text |
| `-h, --help` | display help for command |

## Examples

### Run a command on a target and print its output

```bash
bdy tg exec command target-identifier "ls -la"
```

### Run SQL on a MySQL target

```bash
bdy tg exec command target-identifier "SELECT 1"
```

### Start a command in background and return the execution id

```bash
bdy tg exec command target-identifier "ls -la" --async
```

### Run a command from a file

```bash
bdy tg exec command target-identifier --file ./query.sql
```

### Print the result with execution metadata as JSON

```bash
bdy tg exec command target-identifier "SELECT 1" --format json
```


---
Original source: https://buddy.works/docs/cli/target/exec/command