Shell基礎筆記 - The Missing Semester of Your CS Education

可惜還是學生的時候不知道有這門好課,雖然已經有些經驗了,但還是想看看這門課能帶給我什麼新東西

快速複習

Basics of the Shell

  • Shell = text-based interface for executing commands.
  • Bash (Bourne Again Shell) is widely used.

Basic Commands

  • date → Show current date/time
  • echo hello → Print text to terminal
  • echo $PATH → Show directories searched for commands
  • which command → Locate the binary file of a command
  • pwd → Show current directory
  • cd /path/to/dir → Change directory
  • ls → List files
  • ls -l → Show detailed file info
  • cd .. → Move up one directory
  • mkdir newdir → Create a directory

File Operations

  • mv old new → Rename/move a file
  • cp source dest → Copy a file
  • rm file → Delete a file
  • man command → Show manual for a command

Redirection & Pipes

  • command > file → Redirect output to a file
  • command >> file → Append output to a file
  • command < file → Use file as input
  • command1 | command2 → Pipe output of one command to another

Examples

  • echo hello > hello.txt → Write “hello” to a file
  • cat hello.txt → Display file contents
  • ls -l / | tail -n1 → Show the last line of ls -l / output
  • curl --head --silent google.com | grep -i content-length | cut -d ' ' -f2 → Get content length of a webpage

Running Commands as Root

  • sudo command → Run command as superuser
  • echo 3 | sudo tee /sys/class/backlight/thinkpad_screen/brightness → Change screen brightness
  • tee displays program output and copying to a file

Exercises

第五題

  • 不能直接寫入,因為#是comment
  • 不能用雙引號因為!是event
  • 要用單引號或是\把驚嘆號轉換成字
  • echo '#!/bin/sh' > semester
  • echo "#\!/bin/sh" > semester

第九題

  • 因為#!這行

第十題

  • ./semester | grep last-modified > last-modified.txt

Shell基礎筆記 - The Missing Semester of Your CS Education
https://f88083.github.io/2025/03/26/Shell/
作者
Simon Lai
發布於
2025年3月26日
許可協議