| name | temp-file-python-analysis |
| description | Inline Python in shell scripts has quoting issues: |
| lastReviewed | 2026-04-30T00:00:00.000Z |
Temp File Python Analysis
The Problem
Inline Python in shell scripts has quoting issues:
python -c "import pandas as pd; df = pd.read_csv('data.csv'); print(df[df['name'] == 'John'])"
python -c "
import json
with open('config.json') as f:
data = json.load(f)
for item in data['items']:
if item['status'] == 'active':
print(item['name'])
"
The Solution
Write to _tmp.py, execute, delete:
cat > _tmp.py << 'EOF'
import pandas as pd
df = pd.read_csv('data.csv')
result = df[df['name'] == 'John']
print(result.to_json(orient='records'))
EOF
python _tmp.py
rm _tmp.py
PowerShell Equivalent
$script = @'
import pandas as pd
df = pd.read_csv('data.csv')
print(df.describe().to_string())
'@
$script | Out-File -FilePath "_tmp.py" -Encoding utf8
python _tmp.py
Remove-Item "_tmp.py"
Benefits
| Approach | Quoting Issues | Debugging | Editor Support |
|---|
Inline -c | Many | Hard | None |
| Heredoc | Some | Medium | Limited |
| Temp file | None | Easy | Full |
Patterns
Quick Data Analysis
cat > _tmp.py << 'EOF'
import pandas as pd
import sys
df = pd.read_csv(sys.argv[1])
print(f"Rows: {len(df)}")
print(f"Columns: {list(df.columns)}")
print(df.dtypes)
EOF
python _tmp.py data.csv
rm _tmp.py
JSON Transformation
cat > _tmp.py << 'EOF'
import json
with open('input.json') as f:
data = json.load(f)
result = [item for item in data if item.get('active')]
with open('output.json', 'w') as f:
json.dump(result, f, indent=2)
EOF
python _tmp.py
rm _tmp.py
When to Apply
- Any multi-line Python in shell context
- Complex string handling
- Data analysis one-offs
- JSON/CSV processing
Tags
data python shell analysis