https://github.com/r0h1th-1dd4e2/linux-workshop
Cheat Sheet for bash scripting
https://github.com/r0h1th-1dd4e2/linux-workshop
bash bash-scripting example shebang tutorial-code
Last synced: 10 months ago
JSON representation
Cheat Sheet for bash scripting
- Host: GitHub
- URL: https://github.com/r0h1th-1dd4e2/linux-workshop
- Owner: R0h1th-1DD4E2
- Created: 2024-10-22T00:45:31.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-22T00:47:51.000Z (over 1 year ago)
- Last Synced: 2025-04-03T13:19:55.856Z (10 months ago)
- Topics: bash, bash-scripting, example, shebang, tutorial-code
- Homepage:
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Bash Scripting
### Shebang
`#!/bin/bash` - This tells the system that the script should be run with the bash shell.
#### Example
```bash
#!/bin/bash
echo "Hello, World!"
```
### Variables in Bash
```bash
#!/bin/bash
name="Alice"
echo "Hello, $name!"
```
### User Input
```bash
#!/bin/bash
echo "What's your name?"
read name
echo "Hello, $name!"
```
### Conditions (if statements)
```bash
#!/bin/bash
echo "Enter a number:"
read num
if [ $num -gt 10 ]; then
echo "Your number is greater than 10!"
else
echo "Your number is 10 or less!"
fi
```
### Common Condition Tests:
| Test | Description |
| ---------------------------------------- | -------------------------------------------- |
| `-d ` | True if `` is a directory. |
| `-f ` | True if `` is a regular file. |
| `-e ` | True if `` exists. |
| `-z ` | True if `` is empty. |
| `-n ` | True if `` is not empty. |
| `"" = ""` | True if strings are equal. |
| `"" != ""` | True if strings are not equal. |
| `-eq`, `-ne`, `-lt`, `-le`, `-gt`, `-ge` | Numeric comparisons. |
| `!` | Negates the condition (logical NOT). |
| `&&` | Logical AND (both conditions must be true). |
| \|\| | Logical OR (any one conditions can be true). |
### Loops
#### For Loop
```bash
#!/bin/bash
for i in {1..5}
do
echo "Number: $i"
done
```
#### While Loop
```bash
#!/bin/bash
counter=1
while [ $counter -le 5 ]
do
echo "Counter: $counter"
((counter++))
done
```
### Function
```bash
#!/bin/bash
greet() {
echo "Hello, $1!"
}
greet "Alice"
greet "Bob"
```
### Comments
```bash
# This is a comment
```