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

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.
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 adalah pilihan terbaik untuk karir IT karena ubiquity dan power-nya.
# 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"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
EOFPipes 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; lsWildcards cocokkan multiple files dengan satu pattern:
# 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# 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# 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# 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'" >> ~/.bashrcShebang (#!) adalah baris pertama script yang memberi tahu sistem interpreter mana yang digunakan:
#!/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| Shortcut | Action |
|---|---|
Ctrl+A | Move to beginning of line |
Ctrl+E | Move to end of line |
Ctrl+L | Clear screen |
Ctrl+R | Search history |
Ctrl+C | Cancel command |
Ctrl+D | Exit shell |
Tab | Auto-complete |
# View history
history
# Run previous command
!!
# Run last command starting with 'ls'
!ls
# Search history
history | grep "grep"
# Clear history
history -crmGunakan command line untuk tugas sederhana, satu kali. Gunakan scripts untuk:
*, ?, [...], {...}$PATH, $HOME, $USERCommand 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.