echo and opreators
This commit is contained in:
60
Linux/Bash Script/1-Echo.md
Normal file
60
Linux/Bash Script/1-Echo.md
Normal file
@@ -0,0 +1,60 @@
|
||||
### 📌 Echo Basic Usage
|
||||
|
||||
```bash
|
||||
echo Hello, world!
|
||||
```
|
||||
|
||||
**Output:**
|
||||
|
||||
```
|
||||
Hello, world!
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 💡 Common Use Cases
|
||||
|
||||
1. **Print plain text:**
|
||||
|
||||
```bash
|
||||
echo This is a message
|
||||
```
|
||||
|
||||
2. **Print environment variables (Linux/macOS):**
|
||||
|
||||
```bash
|
||||
echo $HOME
|
||||
```
|
||||
|
||||
3. **Print environment variables (Windows):**
|
||||
|
||||
```cmd
|
||||
echo %USERNAME%
|
||||
```
|
||||
|
||||
4. **Write to a file:**
|
||||
|
||||
```bash
|
||||
echo "Log entry" >> logfile.txt
|
||||
```
|
||||
|
||||
5. **Suppress newline (Unix/Linux):**
|
||||
|
||||
```bash
|
||||
echo -n "No newline"
|
||||
```
|
||||
|
||||
6. **Use with escape characters (Unix/Linux):**
|
||||
|
||||
```bash
|
||||
echo -e "Line1\nLine2"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### ⚠️ Notes
|
||||
|
||||
* On **Unix-like systems**, `echo` is a shell builtin (e.g., in `bash`, `sh`).
|
||||
* On **Windows**, it’s a command in `cmd.exe`.
|
||||
* Behavior may vary slightly between environments. For complex text handling, consider using `printf` instead in Unix/Linux.
|
||||
|
||||
81
Linux/Bash Script/2-Operators.md
Normal file
81
Linux/Bash Script/2-Operators.md
Normal file
@@ -0,0 +1,81 @@
|
||||
|
||||
# 🖥️ Bash Opreators
|
||||
|
||||
A quick reference guide to essential bash command operators and their usage.
|
||||
|
||||
---
|
||||
|
||||
## `>` — **Write to File (Overwrite)**
|
||||
|
||||
This operator **creates a new file** or **overwrites** the contents of an existing file.
|
||||
|
||||
```bash
|
||||
echo "Hi" > file1
|
||||
```
|
||||
|
||||
📄 *Creates* `file1` and writes `"Hi"` into it. If `file1` already exists, its content is replaced.
|
||||
|
||||
---
|
||||
|
||||
## `>>` — **Append to File**
|
||||
|
||||
Adds content to the **end of an existing file** without deleting what's already there.
|
||||
|
||||
```bash
|
||||
echo "Hi" >> file1
|
||||
```
|
||||
|
||||
📝 *Appends* `"Hi"` to the end of `file1`.
|
||||
|
||||
---
|
||||
|
||||
## `&&` — **AND Operator**
|
||||
|
||||
Runs the **second command only if the first succeeds**.
|
||||
|
||||
```bash
|
||||
apt update && apt upgrade
|
||||
```
|
||||
|
||||
🔗 `apt upgrade` runs only if `apt update` completes successfully.
|
||||
|
||||
---
|
||||
|
||||
## `;` — **Run Multiple Commands**
|
||||
|
||||
Executes **commands sequentially**, regardless of success or failure.
|
||||
|
||||
```bash
|
||||
echo "Hi" > file1 ; cat file1
|
||||
```
|
||||
|
||||
🔄 Both commands are executed one after the other.
|
||||
|
||||
---
|
||||
|
||||
## `*` — **Wildcard (All Matching Files)**
|
||||
|
||||
Matches **all files** that meet the pattern.
|
||||
|
||||
```bash
|
||||
cat file*
|
||||
```
|
||||
|
||||
🌐 Displays the contents of all files starting with `file`.
|
||||
|
||||
---
|
||||
|
||||
## `[ ... ]` — **Specific Character Matching**
|
||||
|
||||
Reads files that match specific characters at the position defined in brackets.
|
||||
|
||||
```bash
|
||||
cat file[1,2,3]
|
||||
```
|
||||
|
||||
📚 Reads `file1`, `file2`, and `file3` (if they exist). Equivalent to:
|
||||
|
||||
```bash
|
||||
cat file1 file2 file3
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user