Langsung ke konten
Skillpedia
Pemrograman umum48k Β· #5886

bash

Fungsinya

Panduan ini mengajarkan cara menulis perintah dan skrip untuk terminal Linux (Bash) dengan lebih aman dan efisien. Panduan ini memberikan contoh cara menghindari kesalahan umum dan menerapkan praktik terbaik.

Kapan dipakai

  • βœ“Saat menulis skrip untuk mengotomatisasi tugas di Linux.
  • βœ“Saat mencoba memperbaiki error pada perintah Bash yang sudah ada.
  • βœ“Saat belajar menggunakan terminal secara lebih mahir.

Link skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/bash/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/bash/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œBagaimana cara memastikan skrip Bash saya menangani nama file yang mengandung spasi dengan benar?”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Panduan ini membandingkan kode 'buruk' dengan kode 'baik' untuk menunjukkan praktik terbaik. Fokusnya meliputi cara menangani variabel, kondisi, perulangan, dan komunikasi antar perintah.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

Bash / Shell: Idiomatic Efficiency Reference

When to Use

  • Use this skill when the task matches this description: Language-specific super-code guidelines for bash.

Table of Contents

  1. Quoting & Word Splitting
  2. Conditionals & Tests
  3. Loops & Iteration
  4. Pipes & Process Substitution
  5. Functions & Return Values
  6. Error Handling
  7. Anti-patterns specific to Bash

1. Quoting & Word Splitting {#quoting}

# ❌ Unquoted variable (word splitting + globbing)
for f in $files; do rm $f; done

# βœ…
for f in "${files[@]}"; do rm -- "$f"; done
# ❌ Unquoted command substitution
path=$(find . -name config)
cat $path  # breaks on spaces

# βœ…
path="$(find . -name config)"
cat "$path"
# ❌ Using backticks for command substitution
result=`echo hello`

# βœ… β€” $() nests cleanly
result=$(echo hello)
# ❌ String comparison without quotes
if [ $var = "hello" ]; then  # breaks if var is empty or has spaces

# βœ…
if [[ "$var" = "hello" ]]; then

Rule: double-quote every $variable and $(command) unless you specifically need splitting.


2. Conditionals & Tests {#conditionals}

# ❌ Single bracket test (POSIX but fragile)
if [ -f "$file" -a -r "$file" ]; then

# βœ… β€” [[ is safer, supports &&/||, no word splitting inside
if [[ -f "$file" && -r "$file" ]]; then
# ❌ Testing command exit status with if [ $? -eq 0 ]
grep -q pattern file
if [ $? -eq 0 ]; then echo "found"; fi

# βœ… β€” test command directly
if grep -q pattern file; then echo "found"; fi
# ❌ String equality with == outside [[
if [ "$a" == "$b" ]; then  # == not POSIX in [ ]

# βœ…
if [[ "$a" == "$b" ]]; then  # Bash
# or POSIX:
if [ "$a" = "$b" ]; then
# ❌ Arithmetic with [ ] and string comparison
if [ "$count" -gt 10 ]; then

# βœ… β€” (( )) for arithmetic
if (( count > 10 )); then

3. Loops & Iteration {#loops}

# ❌ Parsing ls output
for f in $(ls *.txt); do process "$f"; done

# βœ… β€” glob directly
for f in *.txt; do
    [[ -e "$f" ]] || continue  # handle no-match
    process "$f"
done
# ❌ Reading file line-by-line with for
for line in $(cat file.txt); do  # splits on words, not lines

# βœ…
while IFS= read -r line; do
    process "$line"
done < file.txt
# ❌ Seq for counting
for i in $(seq 1 10); do

# βœ… β€” brace expansion (Bash)
for i in {1..10}; do
# or C-style:
for (( i = 1; i <= 10; i++ )); do
# ❌ Processing command output line-by-line with pipe (subshell trap)
count=0
cat file.txt | while read -r line; do
    (( count++ ))  # count resets after loop β€” subshell
done
echo "$count"  # always 0

# βœ… β€” redirect, not pipe
count=0
while IFS= read -r line; do
    (( count++ ))
done < file.txt
echo "$count"

4. Pipes & Process Substitution {#pipes}

# ❌ Chained grep | grep for AND
grep "error" log.txt | grep "timeout"

# βœ… β€” single grep with pattern
grep -E "error.*timeout|timeout.*error" log.txt
# or awk for complex logic:
awk '/error/ && /timeout/' log.txt
# ❌ cat + pipe (UUOC β€” Useless Use of Cat)
cat file.txt | grep pattern

# βœ…
grep pattern file.txt
# ❌ Temp file for diff between commands
cmd1 > /tmp/a.txt
cmd2 > /tmp/b.txt
diff /tmp/a.txt /tmp/b.txt
rm /tmp/a.txt /tmp/b.txt

# βœ… β€” process substitution
diff <(cmd1) <(cmd2)
# ❌ Ignoring pipe failures (only last command's exit code)
false | true
echo $?  # 0 β€” false's failure hidden

# βœ…
set -o pipefail
false | true
echo $?  # 1

5. Functions & Return Values {#functions}

# ❌ Using return for string values
get_name() {
    return "Alice"  # return is for exit codes (0-255)
}

# βœ… β€” echo + capture
get_name() {
    echo "Alice"
}
name=$(get_name)
# ❌ Global variables modified inside functions
result=""
compute() { result="done"; }

# βœ… β€” use local, return via stdout
compute() {
    local tmp
    tmp=$(do_work)
    echo "$tmp"
}
result=$(compute)
# ❌ Function keyword (not POSIX)
function my_func {

# βœ…
my_func() {

6. Error Handling {#errors}

# ❌ No error handling β€” script continues after failure
cd /some/dir
rm -rf *  # if cd fails, deletes from wrong directory

# βœ…
set -euo pipefail

cd /some/dir || { echo "cd failed" >&2; exit 1; }
rm -rf ./*
# ❌ No cleanup on exit
tmpfile=$(mktemp)
# ... script might exit early, leaving tmpfile


…
Pertanyaan umum

Apa itu 'Bash'?

Bash adalah program yang menginterpretasikan perintah yang Anda ketik di terminal Linux. Ibaratnya, Bash adalah penerjemah antara Anda dan komputer.

Mengapa 'double quotes' penting?

Double quotes mencegah Bash salah menafsirkan spasi atau karakter khusus pada nama file atau variabel, sehingga error dapat dihindari.

Apa arti 'set -euo pipefail'?

Ini adalah pengaturan yang membuat skrip Anda lebih aman, dengan cara menghentikannya segera jika sebuah perintah gagal atau jika variabel yang belum didefinisikan digunakan.

Detail

Deskripsi asli dari pembuat

Language-specific super-code guidelines for bash.

repo: sickn33/agentic-awesome-skills

Popularitas

Bintang GitHub
48k
Peringkat
#5886
Lisensi
MIT
Diperbarui
15/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa