C.W.K.
Stream
Lesson 05 of 14 · published

Remote Execution & Quoting

~12 min · remote-exec, quoting, heredoc

Level 0Pinger
0 XP0/101 lessons0/12 achievements
0/150 XP to next level150 XP to go0% complete

일회성 명령

인터랙티브 SSH 세션 항상 필요 X. ssh host 'command' 이 원격에서 명령 실행하고 exit. 병렬 패턴과 결합하면 fleet 명령 셸.

Quoting 함정

SSH 의 quoting 이 대부분 놀람 일어나는 곳. 규칙 — single quote = "원격 쪽에서 평가". Double quote = "로컬 셸 먼저 확장, 그 결과 원격으로". 의심 들면 single quote. 로컬 변수를 원격 명령에 박아야 하면 heredoc 패턴.

Code

Single vs double quote·bash
# Single quotes — evaluated on the remote machine
ssh office 'echo $HOME'
# Output: /Users/you_username (remote user's home)

# Double quotes — evaluated locally first!
ssh office "echo $HOME"
# Output: /Users/you_username (your LOCAL home — possibly the wrong answer)

# Want both? Escape the dollar to delay expansion to remote
ssh office "echo \$HOME"

# When in doubt, echo the command first to see what gets sent
echo "echo $HOME"
echo 'echo $HOME'
Heredoc 통한 multi-line·bash
# Quoted heredoc — entire block runs on the remote unchanged
ssh office <<'EOF'
echo "Hostname: $(hostname)"
echo "Uptime: $(uptime)"
echo "Disk: $(df -h / | tail -1)"
EOF

# Unquoted heredoc — local variables expand first
NOW=$(date)
ssh office <<EOF
echo "From local: ${NOW}"
echo "From remote: $(hostname)"
EOF
# Note: $(hostname) here runs LOCALLY — usually not what you want
# Use the quoted form 99% of the time

External links

Exercise

어느 머신에서든 ssh office 'echo $HOME'ssh office "echo $HOME" 둘 다 돌려. 차이 메모. 그다음 원격에서 명령 셋 (hostname, df, uptime) 돌리는 heredoc 구성 — <<'EOF' 형식 사용. 끊임없이 쓸 fleet 자동화 primitive.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.