Bash and Zsh

What is Bash

Bash, short for “Bourne-Again SHell”, is a Unix shell and command language that is widely used on Unix and Linux operating systems. It was written by Brian Fox for the GNU Project as a free software replacement for the original Bourne shell (sh), and has since become the default shell on most Linux distributions as well as on macOS.

As a command language, Bash allows users to interact with a Unix-based operating system through a command-line interface. Users can enter commands to launch applications, manipulate files and directories, run scripts, and perform other system-related tasks. Bash also provides features such as command history, tab completion, and input and output redirection, which can help users work more efficiently.

Bash is also a scripting language, which means that users can write Bash scripts to automate repetitive tasks or perform more complex operations. Bash scripts can be used for a variety of purposes, such as system administration, data processing, and web development, among others.

Overall, Bash is a powerful and versatile tool for working with Unix-based operating systems, and is widely used by developers, system administrators, and power users alike.

What’s the different with Zsh

Zsh, short for “Z Shell”, is another Unix shell and command language, and is also widely used on Unix and Linux operating systems. Zsh was developed as an extended version of the Bourne shell (sh) and the Korn shell (ksh), and offers many features and enhancements that are not available in Bash.

One major difference between Bash and Zsh is that Zsh has better tab completion and correction capabilities. Zsh provides more advanced tab completion, which allows for more flexible and efficient command-line usage. Zsh also has a feature called “correction,” which automatically corrects common spelling mistakes and other errors in commands.

Another difference is that Zsh has a built-in module system that allows for easy customization and extension of the shell. Users can add or remove features and functionalities by loading or unloading modules, making it easier to tailor the shell to their specific needs.

Finally, Zsh has a more modern and user-friendly configuration system, which makes it easier to customize and configure the shell to the user’s preferences.

Overall, while Bash and Zsh share many similarities, Zsh provides more advanced features and customization options, making it a popular choice among power users and developers who require a high level of flexibility and control over their shell environment.

What’s the different with JavaScript

Bash and JavaScript are two very different programming languages that serve different purposes.

Bash is primarily a scripting language used for automating tasks and running system commands on Unix and Linux operating systems. It is typically used in a command-line interface and has a syntax optimized for executing commands and manipulating files and directories. Bash is often used for tasks such as system administration, automation, and data processing.

JavaScript, on the other hand, is a high-level programming language that is used primarily for web development. It is executed within web browsers and is used to add interactivity and dynamic behavior to web pages. JavaScript is also used on the server side with Node.js to build full-stack web applications, as well as for developing mobile and desktop applications.

While both Bash and JavaScript are used for programming, their syntax, use cases, and features are very different. Bash is focused on running system commands and automating tasks, while JavaScript is focused on building interactive and dynamic web applications.

File/directory permissions

In Linux/Unix systems, every file and directory has a set of permissions that control access to that file or directory. Permissions are determined by three categories: owner, group, and others.

Each category can have three types of permissions: read (r), write (w), and execute (x). These permissions have the following meanings:

Read permission (r): Can read and view the contents of the file or directory. Write permission (w): Can modify the contents of the file or directory, including adding, deleting, and renaming. Execute permission (x): For files, can run executable files; for directories, can enter the directory. Permissions can be represented using numbers or letters. When using letters to represent permissions, r represents read permission, w represents write permission, and x represents execute permission. When using numbers to represent permissions, each permission has a numeric value, with r as 4, w as 2, and x as 1, so rwx is 7 (4+2+1).

For example, a file with permission -rw-r–r– means the owner has read and write permissions while the group and others only have read permission.

Bash commands

  1. ls -l filename: display detailed information about a file or directory. The “-l” option stands for “long” and displays additional information, such as the file or directory’s permissions, owner, group, size, creation date, and modification date.
  2. cd: change the current directory
  3. pwd: display the full path of the current working directory
  4. mkdir: create a new directory
  5. rm: remove files or directories
  6. cp: copy files or directories
  7. mv: move or rename files or directories
  8. cat: view the contents of a file
  9. less: view the contents of a file one page at a time
  10. grep: search for a specified string in a file
  11. find: search for a specified file or directory in the file system
  12. chmod: change the permissions of a file or directory e.g. chmod +x filename
  13. chown: change the owner of a file or directory

Build up Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/bin/bash
#3hours=10800 s

reminder_interval=10

next_reminder=$(($(date +%s) + $reminder_interval))

while true; do

current_time=$(date +%s)

if [ $current_time -ge $next_reminder ]; then
        echo "it's time to drink water"
        next_reminder=$((current_time + $reminder_interval))
fi
sleep 5
done
  1. #!/bin/bash: This is a shebang line that specifies which interpreter should be used to execute the script. In this case, it specifies that the Bash shell should be used.

  2. #3hours=10800 s: This is a comment line that provides a brief explanation of what the script does. It notes that 3 hours equals 10,800 seconds.

  3. reminder_interval=10: This line sets a variable called reminder_interval to 10, which represents the number of seconds between reminders to drink water.

  4. next_reminder=$(($(date +%s) + $reminder_interval)): This line calculates the time of the next reminder to drink water by getting the current time in seconds since the Unix epoch (date +%s) and adding the reminder interval to it. The result is stored in a variable called next_reminder.

  5. while true; do: This starts an infinite loop that will continue until the script is interrupted or killed.

  6. current_time=$(date +%s): This line gets the current time in seconds since the Unix epoch and stores it in a variable called current_time.

  7. if [ $current_time -ge $next_reminder ]; then: This line checks whether the current time is greater than or equal to the next reminder time. If it is, the script will print a message reminding the user to drink water.

  8. echo "it's time to drink water": This line prints a reminder message to the console.

  9. next_reminder=$((current_time + $reminder_interval)): This line updates the next_reminder variable to be the current time plus the reminder interval.

  10. fi: This line closes the if statement.

  11. sleep 5: This line pauses the script for 5 seconds before starting the loop again. This delay is used to prevent the script from consuming too many system resources.

A shell is a command-line interface (CLI) that provides a way for users to interact with an operating system. In Unix and Unix-like systems, such as macOS and Linux, the shell is an interface through which users can enter commands to interact with the operating system. Common shells in Unix systems include Bash, Zsh, Ksh, Tcsh, and others. In macOS, the default shell is Zsh.

Bash scripting cheatsheet

Bash 是什麼

Bash(Bourne-Again SHell)是一種在Linux和其他Unix操作系統上常用的命令行解釋器,它是由Brian Fox和Chet Ramey於1987年開發的。Bash是一種文本界面的操作環境,可以通過在終端窗口中輸入命令來執行各種系統操作,例如檔案管理、系統配置、程序啟動等。Bash支持各種內置命令和腳本語言,這些功能可以用於自動化和編寫各種系統管理任務。

Bash是Linux系統中最常見的shell之一,也是許多Unix系統預設的shell。Bash具有豐富的功能,如命令自動完成、歷史命令查詢、腳本編寫等,使得用戶可以更加高效地使用Linux和其他Unix系統。

與 Zsh 的差異

Zsh(Z shell)是一種強大的命令行解釋器,也是Linux和Unix系統中的一種shell。Zsh與Bash非常相似,兩者都提供了一個強大的命令行界面,可以用於檔案管理、腳本編寫和系統管理等任務。但是,Zsh相對於Bash在以下幾個方面有一些不同之處:

自動補全:Zsh提供更強大的自動補全功能,支持多個輸入源,如歷史命令、檔案、目錄等。

設定管理:Zsh提供更簡單的設定管理方式,包括類似於Oh My Zsh的外掛程式,可以幫助用戶快速定制shell界面和功能。

選項設定:Zsh提供了更多的選項設定,例如更好的警告和錯誤信息、更豐富的Shell Parameter Expansion等。

終端顏色:Zsh內建支援更豐富的終端顏色和風格選項。

總的來說,Zsh和Bash都是強大的命令行解釋器,兩者有很多共同點,但是Zsh在一些方面提供了更強大和便利的功能。當然,哪種shell更適合您取決於您個人的偏好和需求。

跟 javascript 有什麼不同

Bash和JavaScript都是程式語言,但它們有著不同的用途和設計目標,因此有以下幾個不同之處:

用途不同:Bash主要用於在Unix和Linux系統上進行系統管理和自動化任務,而JavaScript主要用於網頁前端和後端開發。

語言特性不同:Bash是一種腳本語言,它具有許多與命令行相關的特性,例如命令行解釋、管道和重定向等。而JavaScript是一種通用的語言,具有面向對象、函數式和事件驅動等特性。

運行環境不同:Bash在Unix和Linux系統上運行,而JavaScript可以在網頁瀏覽器、Node.js和其他JavaScript運行環境中運行。

語言生態系統不同:Bash的生態系統主要集中在Unix和Linux系統中,而JavaScript擁有廣泛的生態系統,包括許多框架、函式庫和工具。

總的來說,Bash和JavaScript都是程式語言,但是它們的設計目標和用途不同,因此擁有不同的語言特性和生態系統。

文件權限

在 Linux/Unix 系統中,每個文件和目錄都有一組權限,控制對該文件或目錄的訪問。權限是由三個類別決定的:所有者、群組和其他人。

每個類別可以有三種權限:讀取(r)、寫入(w)和執行(x)。這些權限的意義如下:

讀取權限(r):可以讀取和查看文件或目錄的內容。 寫入權限(w):可以修改文件或目錄的內容,包括添加、刪除和重命名。 執行權限(x):對於文件,可以運行可執行文件;對於目錄,可以進入目錄。 權限可以用數字或字母表示。使用字母表示權限時,r 表示讀取權限,w 表示寫入權限,x 表示執行權限。使用數字表示權限時,每種權限都有一個數字值,r 為 4,w 為 2,x 為 1,因此 rwx 為 7(4+2+1)。

例如,權限為 -rw-r–r– 的文件表示所有者具有讀寫權限,而群組和其他人僅具有讀取權限。

Bash 命令

echo $SHELL確認目前 macOS 系統使用的 shell chsh -s /bin/bash更改當前的 shell為 Bash shell

  1. ls -l 檔案名: 顯示文件和目錄詳細信息的命令,其中 “-l” 參數表示 “long”,可以顯示文件或目錄的詳細屬性信息。這些詳細信息包括文件/目錄的權限、所有者、所屬組、大小、創建日期、修改日期等。
  2. cd:改變當前目錄。
  3. pwd:顯示當前工作目錄的完整路徑。
  4. mkdir:創建一個新目錄。
  5. rm:刪除文件或目錄。
  6. cp:複製文件或目錄。
  7. mv:移動或重命名文件或目錄。
  8. cat:查看文件的內容。
  9. less:逐頁查看文件內容。
  10. grep:在文件中搜索指定字串。
  11. find:在文件系統中搜索指定文件或目錄。
  12. chmod:更改文件或目錄的權限。 e.g. chmod +x 檔案名;chmod g+x 檔案名,權限類別可以是 u(所有者)、g(群組)或 o(其他人)。使用 + 來添加權限,- 來移除權限,或 = 來設置權限。x 表示執行權限。
  13. chown:更改文件或目錄的所有者。

範例

  1. 新增並編輯腳本 nano stay_hyderated.sh
  2. 執行 ./stay_hyderated.sh
  3. 每隔15秒會顯示it's time to drink water

stay_hyderated.sh腳本內容

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/bin/bash
#3hours=10800 s

reminder_interval=10

next_reminder=$(($(date +%s) + $reminder_interval))

while true; do

current_time=$(date +%s)

if [ $current_time -ge $next_reminder ]; then
        echo "it's time to drink water"
        next_reminder=$((current_time + $reminder_interval))
fi
sleep 5
done

這個範例是一個bash腳本,用來提醒你每隔一段時間就要喝水。腳本首先定義了一個變量reminder_interval,表示提醒的間隔時間,這裡設置為10秒。

接下來,腳本進入一個無限循環中,每次循環都會檢查當前時間是否已經超過了下一個提醒時間。如果已經到達了提醒時間,腳本會輸出一條消息,提示你喝水 it's time to drink water ,然後更新下一個提醒時間為當前時間加上提醒間隔時間。

循環的每次迭代之間,腳本會暫停5秒,以減少系統資源的使用。

等於每個循環會間隔10秒,然後腳本會等待5秒再進入下一個循環,這樣每個循環總共會耗時大約15秒左右(10秒的提醒間隔加上5秒的暫停時間)。

每行說明如下

  1. #!/bin/bash: Shebang是一種特殊的註釋語法,用於指定腳本應使用哪個解譯器來執行。它通常是腳本文件的第一行,並以#!開頭。本行指定了腳本的解釋器,即在執行腳本時使用Bash作為解釋器。

  2. #3hours=10800 s: 第一行指定了腳本的解釋器,即在執行腳本時使用Bash作為解釋器。

  3. reminder_interval=10: 這一行設置reminder_interval變量的值為10,即提醒的間隔時間為10秒。

  4. next_reminder=$(($(date +%s) + $reminder_interval)): 計算下一個提醒時間,它使用$(...)來運行一個子命令。 $(date +%s) 用於獲取當前時間的UNIX時間戳(以秒為單位),然後加上reminder_interval的值來計算下一個提醒時間。

  5. while true; do: 這是一個無限循環的開始。它使用while true來表示進入一個無限循環中,因為 true 總是返回成功。

  6. current_time=$(date +%s): 獲取當前時間的UNIX時間戳,並將其賦值給current_time變量。

  7. if [ $current_time -ge $next_reminder ]; then: 一個if語句,用於檢查是否已經到達下一個提醒時間。-ge 表示大於或等於,如果current_time大於或等於next_reminder,那麼就執行then後面的語句。

  8. echo "it's time to drink water": 如果到達了提醒時間,則輸出一條消息,提示你喝水。

  9. next_reminder=$((current_time + $reminder_interval)): 這一行更新next_reminder的值,以計算下一個提醒時間。它使用$((...))來執行一個算術運算,計算當前時間加上提醒間隔時間的值。

  10. fi: 這是if語句的結束標記。

  11. sleep 5: 這一行使腳本暫停5秒,以減少系統資源的使用。每次循環之間暫停一段時間可以使腳本不會一直忙碌並浪費資源。

  12. done: 這是無限循環的結束。

Shell 是一種命令行界面(Command Line Interface, CLI),是一個人與操作系統之間進行互動的方式。在 Unix 和類 Unix 系統(如 macOS 和 Linux)中,Shell 是一個介面,用戶可以通過命令行輸入命令,從而與操作系統進行互動。

Bash scripting cheatsheet

updatedupdated2023-03-062023-03-06