---
title: "Bash Scripting Examples"
description: "Advanced bash scripting examples for TraceRTM CLI automation"
keywords: [bash, scripting, automation, examples, shell]
---

# Bash Scripting Examples

Advanced bash scripting examples for automating TraceRTM operations.

## Project Setup Script

```bash
#!/bin/bash
# setup-project.sh - Complete project setup

set -e

PROJECT_NAME="$1"
if [ -z "$PROJECT_NAME" ]; then
  echo "Usage: $0 <project-name>"
  exit 1
fi

# Create project
echo "Creating project: $PROJECT_NAME"
PROJECT_ID=$(trace project create "$PROJECT_NAME" --output json | jq -r '.id')
echo "Project created: $PROJECT_ID"

# Switch to project
trace project switch "$PROJECT_ID"

# Create initial items
echo "Creating initial items..."
trace item create --title "Initial Requirement" --view requirements
trace item create --title "Initial Test" --view test-cases

echo "Setup complete!"
```

## Backup Script

```bash
#!/bin/bash
# backup-project.sh - Backup project data

PROJECT_ID="$1"
BACKUP_DIR="${2:-./backups}"

mkdir -p "$BACKUP_DIR"

TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/project_${PROJECT_ID}_${TIMESTAMP}.json"

echo "Backing up project $PROJECT_ID..."
trace export "$PROJECT_ID" --format json --output "$BACKUP_FILE"

echo "Backup saved to: $BACKUP_FILE"
```

## Status Update Script

```bash
#!/bin/bash
# update-status.sh - Batch status updates

STATUS="$1"
VIEW="${2:-requirements}"

# Get all items of specified view
ITEMS=$(trace item list --view "$VIEW" --output json)

# Update each item
echo "$ITEMS" | jq -r '.[].id' | while read item_id; do
  echo "Updating $item_id to $STATUS"
  trace item update "$item_id" --status "$STATUS"
done
```

## Link Creation Script

```bash
#!/bin/bash
# create-traceability.sh - Create traceability links

REQ_FILE="requirements.txt"
TEST_FILE="tests.txt"

# Read requirement and test IDs
while IFS= read -r req_id && IFS= read -r test_id <&3; do
  echo "Linking $req_id to $test_id"
  trace link create \
    --source "$req_id" \
    --target "$test_id" \
    --type verified_by
done < "$REQ_FILE" 3< "$TEST_FILE"
```

## Related Documentation

- [Scripting](./04-scripting) - Scripting basics
- [Commands](./03-commands) - Command reference
