# Executing Shell Commands

lam.shell is a secure shell execution system that allows you to safely execute bash shell commands while providing the ability to chain together any type of business logic. This system enables you to build complex workflows by combining shell commands with control flow mechanisms and configuration management.

{% stepper %}
{% step %}
**Basic `lam.shell` Step Structure**

```javascript
(data) => {
    const script = `#!/bin/bash
    set -e
    
    echo "Hello from lam.shell"
    ls -la /data/
    `;
    
    return {
        "lam.shell": {
            "script": script,
            "timeout": 300
        }
    };
}
```

{% endstep %}

{% step %}
**File Processing Step with Binary Data**

```javascript
(data) => {
    const fileNames = data.input.files || [];
    
    const script = `#!/bin/bash
    set -e
    
    echo "Processing ${fileNames.length} zip files..."
    
    # List available files
    ls -al /data/input/
    
    # Process each zip file
    for file in /data/input/*.zip; do
        if [ -f "$file" ]; then
            filename=$(basename "$file" .zip)
            echo "Extracting $filename..."
            
            # Create extraction directory
            mkdir -p "/data/output/$filename"
            
            # Extract zip file
            unzip "$file" -d "/data/output/$filename"
            
            # List extracted contents
            echo "Contents of $filename:"
            ls -la "/data/output/$filename"
        fi
    done
    
    echo "File processing completed"
    `;
    
    return {
        "lam.shell": {
            "script": script,
            "binaryDataIds": data.step_6.response, // Pass binary data IDs
            "environment": {
                "SFDX_DISABLE_AUTOUPDATE": "true",
                "SFDX_USE_GENERIC_UNIX_KEYCHAIN": "true"
            },
            "timeout": 600
        }
    };
}
```

{% endstep %}

{% step %}
**Configuration-Driven Step**

```javascript
(data) => {
    const script = `#!/bin/bash
    set -e
    
    # Use configuration values
    API_URL="{{config.apiUrl}}"
    API_KEY="{{config.apiKey}}"
    BATCH_SIZE="{{config.batchSize}}"
    
    echo "Starting data processing with batch size: $BATCH_SIZE"
    
    # Validate configuration
    if [ -z "$API_URL" ]; then
        echo "Error: API URL not configured"
        exit 1
    fi
    
    # Process data in batches
    TOTAL_RECORDS=$(wc -l < /data/input/data.csv)
    BATCHES=$(( (TOTAL_RECORDS + BATCH_SIZE - 1) / BATCH_SIZE ))
    
    echo "Processing $TOTAL_RECORDS records in $BATCHES batches"
    
    for ((i=1; i<=BATCHES; i++)); do
        START_LINE=$(( (i-1) * BATCH_SIZE + 1 ))
        END_LINE=$(( i * BATCH_SIZE ))
        
        echo "Processing batch $i (lines $START_LINE-$END_LINE)"
        
        # Extract batch
        sed -n "${START_LINE},${END_LINE}p" /data/input/data.csv > /tmp/batch_$i.csv
        
        # Send batch to API
        curl -X POST "$API_URL/batch" \
             -H "Authorization: Bearer $API_KEY" \
             -H "Content-Type: text/csv" \
             --data-binary @/tmp/batch_$i.csv \
             -o /data/output/batch_${i}_response.json
        
        # Clean up temp file
        rm /tmp/batch_$i.csv
    done
    
    echo "Batch processing completed"
    `;
    
    return {
        "lam.shell": {
            "script": script,
            "environment": {
                "CURL_CA_BUNDLE": "/etc/ssl/certs/ca-certificates.crt"
            },
            "timeout": 1800
        }
    };
}
```

{% endstep %}
{% endstepper %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.laminar.run/advanced/executing-shell-commands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
