From f12f6ce9731c4d84dc73d4aa9ba7abefaa287814 Mon Sep 17 00:00:00 2001 From: TrudeEH Date: Wed, 23 Apr 2025 15:09:15 +0100 Subject: [PATCH] Use dash for scripts instead of bash; Fix typo --- content/.obsidian/workspace.json | 10 +++++----- content/notes/firewall.md | 2 +- content/notes/linux/index.md | 30 +++++++++++++++++++----------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/content/.obsidian/workspace.json b/content/.obsidian/workspace.json index 78926e3..5ee7dc1 100644 --- a/content/.obsidian/workspace.json +++ b/content/.obsidian/workspace.json @@ -14,12 +14,12 @@ "state": { "type": "markdown", "state": { - "file": "drafts/bash.md", + "file": "notes/firewall.md", "mode": "source", "source": false }, "icon": "lucide-file", - "title": "bash" + "title": "firewall" } } ] @@ -218,13 +218,14 @@ }, "active": "b1ce9fc55dcb67b6", "lastOpenFiles": [ - "Pasted image 20250422121610.png", + "notes/firewall.md", + "drafts/bash.md", "notes/linux/index.md", + "Pasted image 20250422121610.png", "Pasted image 20250422122034.png", "notes/index/index.md", "Pasted image 20250422123058.png", "Pasted image 20250422122140.png", - "drafts/bash.md", "notes/c-language.md", "drafts/gobject.md", "drafts/lua.md", @@ -235,7 +236,6 @@ "notes/gdb.md", "notes/flask.md", "notes/encryption.md", - "notes/firewall.md", "notes/compiling.md", "notes/c-snippets.md", "search.md", diff --git a/content/notes/firewall.md b/content/notes/firewall.md index 51846f0..eb56205 100644 --- a/content/notes/firewall.md +++ b/content/notes/firewall.md @@ -9,7 +9,7 @@ tags: author: TrudeEH showToc: true --- -A firewall monitors and controls all incoming and outgoing network traffic, and can be implemented in the hardware or software level. +A firewall monitors and controls all incoming and outgoing network traffic, and can be implemented at the hardware or software level. ## See All Open Ports diff --git a/content/notes/linux/index.md b/content/notes/linux/index.md index d9cd6df..10569a5 100644 --- a/content/notes/linux/index.md +++ b/content/notes/linux/index.md @@ -206,11 +206,11 @@ The GPU still has a device file, which is used by the Kernel's DRM and Mesa, but ## Shell (`bash`) -The kernel by itself isn't intractable, so a shell is needed for the user to be able to execute programs and run commands. Bash is not only a prompt, but also an interpreter for its own programming language, which can be used to write scripts and automate tasks. +The kernel by itself isn't intractable, so a shell is needed for the user to be able to execute programs and run commands. `bash` is not only a prompt, but also an interpreter for its own programming language, which can be used to write scripts and automate tasks. ### Commands -Bash performs actions through commands. A shell command consists of the command itself, followed by its arguments. +`bash` performs actions through commands. A shell command consists of the command itself, followed by its arguments. ```bash command "arg1" "arg2" @@ -218,14 +218,18 @@ command "arg1" "arg2" If the first word of a command is a reserved word, bash handles the command, otherwise, it searches for an executable on the system's `$PATH`, a list of directories where a binary could be located. -### Bash Scripts +### Shell Scripts + +While `bash` is typically used as the login shell, it is slow for script execution. For this reason, many distributions (such as Debian) use `dash` to run `sh` scripts (symlink `/bin/sh` to `/bin/dash`), which is a faster alternative to `bash`, designed specifically to execute scripts and to be strictly POSIX compliant. + +When writing scripts, it's a good practice to follow the POSIX standard, to make them compatible with most shells, older systems, and different distributions, while taking advantage of `dash`'s faster performance. Keep in mind that `dash` is not `bash` compatible, and some `bash` commands might not work on `dash`. Example `script.sh`: ```bash -#! /bin/bash +#! /bin/sh -echo "Bash Script" +echo "Shell Script" ``` Execute the script: @@ -235,15 +239,15 @@ chmod u+x script.sh # Give the script execution permission (to its owner) ./script.sh # Run the script ``` +After writing a script, use `shellcheck` to check for issues. + ### Learning -There are some commands available to search for documentation: - ```bash -tldr command # Examples of how to use the command (external tool) -help command # Short documentation for the command -man command # Full documentation for the command -man bash # Bash documentation; Includes most built-in commands +tldr command # Examples of how to use the command (external tool) +help command # Short documentation for the command +man command # Full documentation for the command +man dash # Dash documentation; Includes most built-in commands ``` > To get started, use `help` by itself, which provides a list of the built-in commands available in bash. @@ -253,6 +257,8 @@ For example, suppose you want to learn how to write an `if` statement in `bash`: ![Pasted image 20250422123058](../../Pasted%20image%2020250422123058.png) As suggested by `tldr`, to see all possible conditions, use the `man test` command. +> If you are writing scripts using `dash`, it's recommended to read the `dash` manual instead: `man dash`. The `if [[ ]]` syntax is exclusive to `bash`, for example, and should be replaced with `if [ ]` to use with `dash`. + ### Compiling Dependencies: @@ -286,6 +292,8 @@ sudo apt install ../bash*.deb Bash includes a set of *builtins*: Command-line utilities that come within bash itself. However, these are very limited, and to actually make the computer useful, more programs are needed. +> To learn more about a program, use: `man program_name`. + ### Coreutils *Coreutils* are a collection of small command-line utilities, which are essential for text, file and shell manipulation.