Community라이팅 & 에디팅github.com

Jamie-BitFlight/bash-portability

POSIX 호환성, 이식 가능한 셸 스크립트 작성, 크로스-셸 호환성, bashism 및 shebang 선택에 대한 지침을 제공합니다.

bash-portability란 무엇인가요?

이 스킬은 개발자가 다양한 셸(bash, sh, dash, zsh)과 시스템 간에 작동하는 이식성이 뛰어난 셸 스크립트를 작성하는 데 필요한 핵심 지침을 제공합니다. POSIX 호환 패턴과 bash 전용 기능의 특징을 상세히 비교하여, 스크립트의 목적과 실행 환경에 따라 최적의 shebang 선택과 코딩 방식을 결정할 수 있도록 돕습니다. Shebang 선택, POSIX 대 Bash 기능 매트릭스, 조건문, 문자열 처리, 파일 읽기 등 주요 패턴을 실제 코드 예제와 함께 설명하며, 이식성 저하를 유발하는 일반적인 함정도 다룹니다. 또한 `echo` vs `printf` 사용, 변수 할당, 배열 흉내 내기 등 세부적인 이식성 문제를 해결하는 방법을 제시하여, 사용자가 초보자부터 고급 개발자까지 활용할 수 있는 실용적인 레퍼런스를 제공합니다.

지원 대상~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Jamie-BitFlight/claude_skills/tree/main/plugins/bash-development/skills/bash-portability

Installed? Explore more 라이팅 & 에디팅 skills: steipete/notion, langchain-ai/langchain, bytedance/podcast-generation · View all 6 →

즐겨 사용하는 AI에게 물어보기

이 에이전트 스킬이 미리 로드된 새 채팅을 엽니다.

문서

Bash 이식성

이식성 있는 POSIX 호환 스크립트를 작성하고, 필요에 따라 bash 전용 기능을 활용하는 방법에 대한 지침입니다.

Shebang 선택

Bash 스크립트: #!/usr/bin/env bash 사용

#!/usr/bin/env bash

이유: PATH에서 bash를 검색하므로 bash의 위치가 다른 시스템에서도 작동합니다.

POSIX 스크립트: #!/bin/sh 사용

#!/bin/sh

이유: bash 기능이 필요하지 않을 때 최대 이식성을 제공합니다. 많은 시스템에서 /bin/sh는 dash나 다른 POSIX 셸입니다.

직접 경로가 필요할 때

#!/bin/bash

다음 경우에만 사용: 시스템 요구 사항이 bash 위치를 보장하거나, 보안 정책에서 절대 경로를 요구하는 경우.

POSIX vs Bash 기능 매트릭스

기능POSIXBash권장 사항
[[ ]]아니오예POSIX에서는 [ ] 사용
(( ))아니오예[ ]와 -eq 등을 사용
배열아니오예위치 매개변수 또는 파일 사용
local부분예일반적으로 안전
${var,,}아니오4+POSIX에서는 tr 사용
<<<아니오예echo | cmd 사용
=~ 정규식아니오예grep 또는 expr 사용
source아니오예. (dot) 명령어 사용
function f()아니오예f()만 사용
$'...'아니오예printf 사용
{1..10}아니오예seq 또는 while 루프 사용

POSIX 호환 패턴

조건문

# POSIX - 적절한 인용과 함께 [ ] 사용
if [ -f "$file" ]; then
    echo "파일이 존재합니다"
fi

# 문자열 비교
if [ "$var" = "value" ]; then
    echo "일치"
fi

# 숫자 비교
if [ "$num" -gt 10 ]; then
    echo "큼"
fi

# 복합 조건
if [ -f "$file" ] && [ -r "$file" ]; then
    echo "읽기 가능한 파일"
fi

대소문자 변환 (POSIX)

# 소문자로
lower=$(echo "$string" | tr '[:upper:]' '[:lower:]')

# 대문자로
upper=$(echo "$string" | tr '[:lower:]' '[:upper:]')

부분 문자열 연산 (POSIX)

# 부분 문자열 가져오기 - expr 또는 cut 사용
substr=$(expr "$string" : '.\{3\}\(.\{5\}\)')  # 4-8번째 문자
substr=$(echo "$string" | cut -c4-8)

# 문자열 길이
length=$(expr length "$string")
length=${#string}  # 이는 실제로 POSIX입니다

파일 읽기 (POSIX)

# 한 줄씩 읽기
while IFS= read -r line; do
    echo "$line"
done < "$file"

# 전체 파일 읽기 (cat 없이)
content=$(cat "$file")  # cat은 POSIX입니다

명령어 치환

# 최신 문법 (POSIX에서도 선호)
result=$(command)

# 레거시 문법 (피하세요)
result=`command`

# 중첩 (최신 문법이 더 나은 이유)
result=$(echo $(date))      # 명확함
result=`echo \`date\``      # 이스케이프 지옥

사용할 가치가 있는 Bash 전용 기능

이식성이 필요하지 않을 때, 다음 bash 기능은 코드 품질을 향상시킵니다:

확장 테스트 [[ ]]

# 패턴 매칭
[[ "$file" == *.txt ]]

# 정규식 매칭
[[ "$input" =~ ^[0-9]+$ ]]

# 단어 분할 걱정 없음
[[ -f $file ]]  # 인용은 선택 사항 (그러나 여전히 권장)

# 내부에서 논리 연산자
[[ -f "$file" && -r "$file" ]]

배열

# 인덱스 배열
declare -a files=()
files+=("one.txt")
files+=("two.txt")
for f in "${files[@]}"; do
    process "$f"
done

# 연관 배열 (Bash 4+)
declare -A config
config[host]="localhost"
config[port]="8080"

매개변수 확장

# 기본값
"${var:-default}"

# 대소문자 변환 (Bash 4+)
"${var,,}"  # 소문자
"${var^^}"  # 대문자

# 부분 문자열
"${var:0:10}"  # 처음 10자
"${var: -5}"   # 마지막 5자

# 검색/바꾸기
"${var//old/new}"

Here 문자열

# Bash
read -r var <<< "input string"

# POSIX 동등
var=$(echo "input string")

프로세스 치환

# Bash - 두 명령어 출력 비교
diff <(sort file1) <(sort file2)

# POSIX 동등 (임시 파일 사용)
sort file1 > /tmp/sorted1
sort file2 > /tmp/sorted2
diff /tmp/sorted1 /tmp/sorted2

셸 유형 감지

# Bash에서 실행 중인지 확인
if [ -n "${BASH_VERSION:-}" ]; then
    echo "Bash에서 실행 중"
fi

# Bash 버전 확인하여 기능 확인
if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
    echo "Bash 4+ 사용 가능"
fi

# 일반적인 셸 감지
case "${SHELL##*/}" in
    bash) echo "bash" ;;
    zsh)  echo "zsh" ;;
    *)    echo "기타" ;;
esac

이식성 유틸리티 함수

코드 예제

이식성 결정 가이드

POSIX를 사용할 때:

  • 스크립트가 최소 시스템(컨테이너, 임베디드)에서 실행됨
  • 대상에 dash, ash 또는 busybox sh가 포함됨
  • 최대 호환성이 요구됨
  • 스크립트가 시스템 초기화의 일부임

Bash를 사용할 때:

  • 대상 시스템에 bash가 있음이 보장됨
  • 배열, 연관 배열 또는 정규식이 필요함
  • 복잡한 문자열 조작이 필요함
  • 코드 명확성이 크게 향상됨
  • 대화형 기능이 필요함

일반적인 이식성 함정

echo vs printf

# 문제가 있음 - 동작이 다양함
echo -n "개행 없음"
echo -e "탭\t포함"

# 이식성 있음
printf '%s' "개행 없음"
printf '탭\t포함\n'

변수 할당

# 모든 곳에서 작동
var="value"

# 일부 셸에서 실패할 수 있음
var = "value"  # = 주변 공백은 잘못됨

할당과 함께 export 사용

# POSIX - 별도 명령어
var="value"
export var

# Bash/최신 - 결합 (대부분의 환경에서 작동)
export var="value"

배열이 없는 배열형 연산

# 위치 매개변수 사용
set -- "item1" "item2" "item3"
for item in "$@"; do
    echo "$item"
done

# 또는 IFS 기반 분할
items="item1:item2:item3"
IFS=':' read -r item1 item2 item3 <<EOF
$items
EOF

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

Jamie-BitFlight/agentskill-kaizen-meta-docs

Agentskill kaizen plugin documentation index. Load when needing to read about cross-platform notes, improvement plans, or DuckDB integration.

Jamie-BitFlight/bash-51-features

Bash 5.1 release features and improvements with practical examples. Use when working with Bash 5.1 features, epoch time variables, redirection enhancements, or when user asks about Bash 5.1 changes, new features, or version-specific capabilities.

Jamie-BitFlight/bash-52-features

Bash 5.2 release features and improvements with practical examples. Use when working with Bash 5.2 features, variable handling enhancements, readline improvements, or when user asks about Bash 5.2 changes, new features, or version-specific capabilities.

Jamie-BitFlight/bash-53-features

Bash 5.3 release features and improvements with practical examples. Use when working with Bash 5.3 features, new command substitution, GLOBSORT, loadable builtins, or when user asks about Bash 5.3 changes, new features, or version-specific capabilities.

Jamie-BitFlight/bash-development

This skill should be used when the user asks to "write a bash script", "create a shell script", "implement bash function", "parse arguments in bash", "handle errors in bash", or mentions bash development, shell scripting, script templates, or modern bash patterns.

Jamie-BitFlight/bash-lint

This skill should be used when the user asks to "lint bash script", "run shellcheck", "format shell script", "use shfmt", "fix shellcheck errors", or mentions shell script linting, formatting, code quality, or pre-commit hooks for bash.

Jamie-BitFlight/bash-logging

This skill should be used when the user asks to "add logging to bash script", "colorize output", "implement log levels", "CI/CD sections", "terminal colors in bash", or mentions logging functions, emoji output, collapsible CI sections, or shlocksmith.

Jamie-BitFlight/bash-testing

This skill should be used when the user asks to "test bash script", "write shell tests", "use shunit2", "use shellspec", "create test suite for bash", or mentions unit testing, test frameworks, mocking, or test-driven development for shell scripts.

Jamie-BitFlight/brainstorming-skill

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or when users request help with ideation, marketing, and strategic planning. Explores user intent, requirements, and design before implementation using research-validated prompt patterns.

Jamie-BitFlight/clang-format

Configure clang-format code formatting. Use when: user mentions clang-format or .clang-format, analyzing code style/patterns, creating/modifying formatting config, troubleshooting formatting, brace styles/indentation/spacing/alignment/pointer alignment, or codifying conventions.

Jamie-BitFlight/commitlint

When setting up commit message validation for a project. When project has commitlint.config.js or .commitlintrc files. When configuring CI/CD to enforce commit format. When extracting commit rules for LLM prompt generation. When debugging commit message rejection errors.

Jamie-BitFlight/conventional-commits

When writing a git commit message. When task completes and changes need committing. When project uses semantic-release, commitizen, git-cliff. When choosing between feat/fix/chore/docs types. When indicating breaking changes. When generating changelogs from commit history.

Jamie-BitFlight/dasel-reference

Use when querying, modifying, or converting JSON, YAML, TOML, XML, CSV, HCL, or INI with dasel v3. Complete reference for selectors, functions, conditionals, variables, spread operator, type casting, and format-specific patterns.

Jamie-BitFlight/data-exploration

Use when exploring unknown structured data files with dasel v3 — discover schema, list keys, find nested values, sample arrays, identify data types across JSON, YAML, TOML, XML, CSV, HCL, INI formats

Jamie-BitFlight/data-transformation

Use when modifying, converting, or transforming structured data with dasel v3 — in-place mutations, format conversion, batch operations, array manipulation, object construction, and merge patterns across JSON, YAML, TOML, XML, CSV, HCL, INI

Jamie-BitFlight/delegate

Decompose substantive work into phases, dispatch each phase to a sub-agent, and adjudicate what comes back. Use whenever a request asks for implementation, investigation, a fix, a review, or any change to files — including small ones — and whenever you are about to read source or run a diagnostic yourself instead of handing it off. Also use when a report from a sub-agent needs judging, when a phase needs re-dispatching, or when a user names one instance of a pattern. Does not apply when your own prompt begins "Your ROLE_TYPE is sub-agent." — then follow references/sub-agent-contract.md instead.

Jamie-BitFlight/enterprise-hibernate-hbm

Dasel v3 query patterns for Hibernate .hbm.xml mapping files — entity-table binding, Java property-to-column extraction, one-to-many set/list/bag relationship tracing, many-to-one foreign key discovery, batch scanning across 60+ HBM files. Use when querying Hibernate ORM class mappings, extracting schema metadata from Java persistence layer, or auditing entity-column relationships in enterprise legacy codebases.

Jamie-BitFlight/enterprise-installanywhere

Dasel v3 query patterns for InstallAnywhere .iap_xml installer definitions — use when querying action sequences, discovering variables, resolving platform conditions, navigating panels, or comparing installer variants. Files are 2.5+ MB, 65,000+ lines — too large for context reads, requires structural dasel queries.

Jamie-BitFlight/enterprise-maven-pom

Dasel v3 selector patterns for Maven POM XML files — use when querying dependency versions, filtering by groupId or scope, extracting module hierarchy from parent POMs, or detecting version conflicts across enterprise multi-module Java projects. Load this skill when working with pom.xml files using dasel.

Jamie-BitFlight/enterprise-spring-xml

Dasel v3 selectors for Spring bean factory XML — use when querying any Spring ApplicationContext XML for bean discovery, dependency wiring, JMS destination mapping, property injection extraction, or cross-bean reference tracing. Load this skill before writing dasel selectors against Spring bean XML files (applicationContext.xml, *_beans.xml, spring-*.xml).

관련 스킬