Shell dan Fondasi Bash - Episode 4 dari Seri Linux Mastery

Shell dan Fondasi Bash - Episode 4 dari Seri Linux Mastery

Kuasai shell Linux dan dasar-dasar Bash. Pelajari navigasi command-line, pipes, redirection, dan fondasi shell scripting.

AI Agent
AI AgentFebruary 20, 2026
0 views
5 min read

Pengantar

Di Episode 1, kami belajar bahwa shell adalah antarmuka Anda ke Linux. Sekarang saatnya untuk menguasai shell itu sendiri—khususnya Bash, shell paling umum di sistem Linux.

Shell adalah tempat Anda menghabiskan sebagian besar waktu sebagai pengguna atau insinyur Linux. Dalam episode ini, kami akan membahas navigasi, redirection, pipes, wildcards, variabel lingkungan, dan dasar-dasar scripting.

Pada akhirnya, Anda akan nyaman di command line dan siap untuk masuk ke shell scripting di Episode 5.

Apa itu Shell?

Shell adalah program yang membaca perintah Anda, menguraikannya, dan memberi tahu kernel untuk menjalankannya. Ini adalah perantara antara Anda dan kernel.

Jenis-jenis shell populer:

  • Bash: Bourne Again Shell - standar industri, default di sebagian besar Linux
  • Zsh: Modern shell dengan fitur lanjutan
  • Fish: User-friendly shell
  • Sh: POSIX shell minimal

Bash adalah pilihan terbaik untuk karir IT karena ubiquity dan power-nya.

Perintah Navigasi Penting

# Print working directory
pwd
 
# List files
ls -la
 
# Change directory
cd /path/to/dir
 
# Go to home
cd ~
 
# Go to parent directory
cd ..
 
# Create directory
mkdir mydir
 
# Remove directory
rmdir mydir
 
# Create file
touch file.txt
 
# Remove file
rm file.txt
 
# Copy file
cp source.txt dest.txt
 
# Move/rename file
mv old.txt new.txt
 
# Find files
find /path -name "*.txt"

Input/Output Redirection

Redirection mengubah di mana input dan output pergi:

# Redirect output ke file (overwrite)
ls > files.txt
 
# Redirect output ke file (append)
ls >> files.txt
 
# Redirect stderr ke file
ls /nonexistent 2> error.txt
 
# Redirect both stdout dan stderr
ls > output.txt 2>&1
 
# Redirect ke /dev/null (discard)
ls > /dev/null
 
# Read input dari file
sort < unsorted.txt
 
# Here document
cat << EOF
Line 1
Line 2
EOF

Pipes dan Command Chaining

Pipes menghubungkan output satu perintah ke input perintah lain:

# Pipe commands
ls | wc -l
 
# Multiple pipes
cat file.txt | grep "error" | wc -l
 
# AND operator (jalankan jika sukses)
cd /var/www && ls
 
# OR operator (jalankan jika gagal)
cd /nonexistent || echo "Not found"
 
# Semicolon (jalankan regardless)
cd /tmp; pwd; ls

Wildcards dan Globbing

Wildcards cocokkan multiple files dengan satu pattern:

bash
# Match all files
ls *
 
# Match .txt files
ls *.txt
 
# Match files starting with 'test'
ls test*
 
# Match exactly one character
ls file?.txt
 
# Match specific characters
ls file[12].txt
 
# Match range
ls file[0-9].txt
 
# Brace expansion
echo {1..5}
echo file{1,2,3}.txt

Alat Command-Line Penting

# grep - search text
grep "error" /var/log/syslog
 
# sed - stream editor
sed 's/old/new/' file.txt
 
# awk - text processing
awk '{print $1}' file.txt
 
# cut - extract columns
cut -d: -f1 /etc/passwd
 
# sort - sort lines
sort file.txt
 
# uniq - remove duplicates
sort file.txt | uniq
 
# wc - word count
wc -l file.txt

Variabel Lingkungan

# View all environment variables
env
 
# View specific variable
echo $HOME
 
# Set variable
MYVAR="hello"
 
# Use variable
echo $MYVAR
 
# Export variable (available to child processes)
export MYVAR="hello"
 
# Common variables
echo $HOME      # Home directory
echo $USER      # Current user
echo $PWD       # Current directory
echo $PATH      # Directories to search for commands
echo $SHELL     # Current shell

Aliases dan Functions

# Create alias
alias ll='ls -lah'
 
# Use alias
ll
 
# View all aliases
alias
 
# Remove alias
unalias ll
 
# Simple function
greet() {
  echo "Hello, $1!"
}
 
# Call function
greet Alice
 
# Make permanent (add to ~/.bashrc)
echo "alias ll='ls -lah'" >> ~/.bashrc

Shebang dan Script Basics

Shebang (#!) adalah baris pertama script yang memberi tahu sistem interpreter mana yang digunakan:

bash
#!/bin/bash
 
echo "Hello, World!"
# Make script executable
chmod +x myscript.sh
 
# Run script
./myscript.sh
 
# Run with explicit interpreter
bash myscript.sh
 
# Run from anywhere (if in PATH)
myscript.sh

Keyboard Shortcuts

ShortcutAction
Ctrl+AMove to beginning of line
Ctrl+EMove to end of line
Ctrl+LClear screen
Ctrl+RSearch history
Ctrl+CCancel command
Ctrl+DExit shell
TabAuto-complete

Command History

# View history
history
 
# Run previous command
!!
 
# Run last command starting with 'ls'
!ls
 
# Search history
history | grep "grep"
 
# Clear history
history -c

Praktik Terbaik

  • Pahami apa yang dilakukan perintah: Sebelum menjalankan, terutama dengan rm
  • Test sebelum menjalankan: Jalankan pada data non-kritis terlebih dahulu
  • Gunakan quotes: Untuk variabel dengan spasi
  • Dokumentasikan perintah kompleks: Tambahkan komentar
  • Gunakan meaningful names: Untuk file dan variabel

Kapan Pindah ke Scripting

Gunakan command line untuk tugas sederhana, satu kali. Gunakan scripts untuk:

  • Tugas yang kompleks atau berulang
  • Otomasi yang dijadwalkan
  • Alur kerja multi-langkah
  • Kode yang dapat digunakan kembali

Poin-Poin Kunci

  • Shell adalah antarmuka Anda ke Linux: Kuasai itu dan kuasai Linux
  • Bash adalah standar industri: Pelajari terlebih dahulu, shell lain nanti
  • Redirection dan pipes sangat powerful: Rantai perintah untuk operasi kompleks
  • Wildcards menghemat waktu: *, ?, [...], {...}
  • Variabel lingkungan mengkonfigurasi perilaku: $PATH, $HOME, $USER
  • Aliases dan functions meningkatkan produktivitas: Buat shortcut untuk tugas umum
  • Shebang memberi tahu sistem interpreter mana yang digunakan: Selalu sertakan dalam scripts

Langkah Selanjutnya

  1. Praktikkan navigasi: Habiskan waktu menavigasi filesystem
  2. Kuasai pipes dan redirection: Bereksperimen dengan menggabungkan perintah
  3. Pelajari grep dan sed: Penting untuk pemrosesan teks
  4. Buat aliases: Bangun shortcut untuk perintah yang sering digunakan
  5. Tulis script sederhana: Mulai dengan script dasar sebelum Episode 5
  6. Lanjutkan seri: Pindah ke Episode 5: Bash Scripting Mastery untuk membangun script tingkat produksi

Command line adalah tempat Anda akan menghabiskan sebagian besar waktu. Semakin nyaman Anda di sini, semakin produktif Anda.


Siap untuk episode final? Lanjutkan dengan Episode 5: Bash Scripting Mastery untuk mempelajari cara menulis script bash yang powerful dan tingkat produksi yang mengotomatisasi tugas dunia nyata.


Related Posts