|
| 1 | +# Common Bash Errors and Fixes |
| 2 | + |
| 3 | +Bash scripting is widely used for automation in Linux, but beginners often face errors that can be confusing. |
| 4 | +This chapter explains common Bash errors, why they occur, and how to fix them. |
| 5 | +Each explanation includes examples to make it easy to understand and apply in real scenarios. |
| 6 | + |
| 7 | +By learning these common mistakes, you can debug scripts faster, write cleaner code, and avoid repeated errors. |
| 8 | + |
| 9 | + |
| 10 | +## 1. Permission Denied |
| 11 | +### Error: |
| 12 | +```bash |
| 13 | +bash: ./script.sh: Permission denied |
| 14 | +``` |
| 15 | +### Cause: |
| 16 | + |
| 17 | +The script file is not executable, which prevents the shell from running it. |
| 18 | + |
| 19 | +### Fix: |
| 20 | +Grant execute permission using: |
| 21 | +```bash |
| 22 | +chmod +x script.sh |
| 23 | +``` |
| 24 | +Then run the script: |
| 25 | +```bash |
| 26 | +./script.sh |
| 27 | +``` |
| 28 | +### Example: |
| 29 | +```bash |
| 30 | +#!/bin/bash |
| 31 | +echo "Hello, world!" |
| 32 | +``` |
| 33 | +Without `chmod +x`, running the script results in an error. |
| 34 | +After granting execute permission, the script runs successfully. |
| 35 | + |
| 36 | +**Explanation:** Linux requires explicit permission to run scripts for security reasons. Always set execute permission before running a new script. |
| 37 | + |
| 38 | + |
| 39 | +## 2. Bad Interpreter or No Such File |
| 40 | +### Error: |
| 41 | +```bash |
| 42 | +bash: ./script.sh: bad interpreter: /bin/bash^M: No such file or directory |
| 43 | +``` |
| 44 | +### Cause: |
| 45 | + |
| 46 | +This occurs when a script is created on Windows. Windows uses carriage return characters (`\r\n`) which Linux cannot read. |
| 47 | + |
| 48 | +### Fix: |
| 49 | + |
| 50 | +Convert the file to Unix format: |
| 51 | +```bash |
| 52 | +dos2unix script.sh |
| 53 | +``` |
| 54 | +Or recreate the file using a Linux text editor such as **nano** or **vim**. |
| 55 | + |
| 56 | +**Explanation:** The first line of a script (`#!/bin/bash`) tells the shell which interpreter to use. Extra Windows characters break this line, causing the error. |
| 57 | + |
| 58 | +## 3. Command Not Found |
| 59 | +### Error: |
| 60 | +```bash |
| 61 | +bash: myscript: command not found |
| 62 | +``` |
| 63 | +### Cause: |
| 64 | +The shell cannot locate the command or script. It might not be in the system PATH or is executed without specifying the path. |
| 65 | + |
| 66 | +### Fix: |
| 67 | +Run the script from its current directory: |
| 68 | +```bash |
| 69 | +./myscript.sh |
| 70 | +``` |
| 71 | +Or add its directory to PATH: |
| 72 | +```bash |
| 73 | +export PATH=$PATH:/path/to/script |
| 74 | +``` |
| 75 | + |
| 76 | +**Explanation:** The shell searches for commands in directories listed in `$PATH`. Scripts outside these directories require an explicit path. |
| 77 | + |
| 78 | +## 4. Syntax Error Near Unexpected Token |
| 79 | +### Error: |
| 80 | +``` |
| 81 | +syntax error near unexpected token `then' |
| 82 | +``` |
| 83 | +### Cause: |
| 84 | + |
| 85 | +A missing `then`, semicolon (`;`) , or incorrect control structure causes Bash to fail parsing the script. |
| 86 | + |
| 87 | +### Fix: |
| 88 | + |
| 89 | +Ensure correct syntax for conditional statements. |
| 90 | + |
| 91 | +**Incorrect:** |
| 92 | +```bash |
| 93 | +if [ $num -gt 10 ] |
| 94 | +echo "Number greater than 10" |
| 95 | +fi |
| 96 | +``` |
| 97 | + |
| 98 | +**Correct:** |
| 99 | +```bash |
| 100 | +if [ $num -gt 10 ]; then |
| 101 | + echo "Number greater than 10" |
| 102 | +fi |
| 103 | +``` |
| 104 | + |
| 105 | +**Explanation:** Bash requires a specific structure for `if` statements: `[condition]; then` followed by commands and closed with `fi`. |
| 106 | + |
| 107 | + |
| 108 | +## 5. Unexpected End of File (EOF) |
| 109 | +### Error: |
| 110 | +``` |
| 111 | +syntax error: unexpected end of file |
| 112 | +``` |
| 113 | +### Cause: |
| 114 | + |
| 115 | +A block (like `if`, `for`, or `while`) or a quote is not properly closed. |
| 116 | + |
| 117 | +### Example: |
| 118 | + |
| 119 | +**Incorrect:** |
| 120 | +```bash |
| 121 | +if [ $num -gt 5 ]; then |
| 122 | + echo "Greater" |
| 123 | +``` |
| 124 | +
|
| 125 | +**Correct:** |
| 126 | +``` |
| 127 | +if [ $num -gt 5 ]; then |
| 128 | + echo "Greater" |
| 129 | +fi |
| 130 | +``` |
| 131 | +
|
| 132 | +**Explanation:** Every opening keyword like `if` or `for` must have a corresponding closing keyword (`fi, done`) for Bash to understand the block. |
| 133 | +
|
| 134 | +
|
| 135 | +## 6. Variable Not Expanding |
| 136 | +## Issue: |
| 137 | +
|
| 138 | +A variable prints empty or does not display its value. |
| 139 | +
|
| 140 | +### Cause: |
| 141 | +
|
| 142 | +The variable is not initialized or not exported correctly. |
| 143 | +
|
| 144 | +### Fix: |
| 145 | +```bash |
| 146 | +username="Nishika" |
| 147 | +echo "User is $username" |
| 148 | +``` |
| 149 | +
|
| 150 | +**Explanation:** Variables must be assigned a value before usage. Otherwise, Bash prints nothing. |
| 151 | +
|
| 152 | +## 7. Integer Expression Expected |
| 153 | +### Error: |
| 154 | +```bash |
| 155 | +[: 5a: integer expression expected |
| 156 | +``` |
| 157 | +### Cause: |
| 158 | +
|
| 159 | +A non-numeric value is used in an arithmetic comparison. |
| 160 | +
|
| 161 | +### Fix: |
| 162 | +```bash |
| 163 | +num=5 |
| 164 | +if [ $num -gt 3 ]; then |
| 165 | + echo "Yes" |
| 166 | +fi |
| 167 | +``` |
| 168 | +
|
| 169 | +**Explanation:** Bash arithmetic comparisons work only with integers. Make sure the variable contains a number. |
| 170 | +
|
| 171 | +## 8. Bad Substitution |
| 172 | +### Error: |
| 173 | +```bash |
| 174 | +bad substitution |
| 175 | +``` |
| 176 | +### Cause: |
| 177 | +
|
| 178 | +Bash-specific syntax is used in a shell that does not support it (like `sh`). |
| 179 | +
|
| 180 | +### Fix: |
| 181 | +
|
| 182 | +Run the script with Bash explicitly: |
| 183 | +```bash |
| 184 | +bash script.sh |
| 185 | +``` |
| 186 | +
|
| 187 | +**Explanation:** Not all shells support Bash extensions. Always use Bash when using advanced syntax. |
| 188 | +
|
| 189 | +
|
| 190 | +## 9. File Redirection Permission Denied |
| 191 | +### Error: |
| 192 | +```bash |
| 193 | +bash: output.txt: Permission denied |
| 194 | +``` |
| 195 | +### Cause: |
| 196 | +
|
| 197 | +The user does not have permission to write to the file or directory. |
| 198 | +
|
| 199 | +### Fix: |
| 200 | +
|
| 201 | +Redirect output to a writable location: |
| 202 | +```bash |
| 203 | +./script.sh > ~/output.txt |
| 204 | +``` |
| 205 | +
|
| 206 | +Or run with elevated permissions: |
| 207 | +```bash |
| 208 | +sudo ./script.sh |
| 209 | +``` |
| 210 | +
|
| 211 | +**Explanation:** Linux enforces strict file permissions. Writing to protected directories requires proper rights. |
| 212 | +
|
| 213 | +
|
| 214 | +## 10. Script Not Found |
| 215 | +### Error: |
| 216 | +```bash |
| 217 | +bash: script.sh: command not found |
| 218 | +``` |
| 219 | +### Cause: |
| 220 | +
|
| 221 | +The script is executed without specifying the relative or absolute path. |
| 222 | +
|
| 223 | +### Fix: |
| 224 | +```bash |
| 225 | +./script.sh |
| 226 | +``` |
| 227 | +
|
| 228 | +Or provide full path: |
| 229 | +```bash |
| 230 | +/path/to/script.sh |
| 231 | +``` |
| 232 | +
|
| 233 | +**Explanation:** Bash will only execute scripts it can locate. Use relative or absolute paths when needed. |
| 234 | +
|
| 235 | +## Practical Example: Loop Over Files |
| 236 | +
|
| 237 | +Beginners often write loops that process a directory itself instead of the files inside it. This is a logical mistake that runs without a syntax error but produces unexpected results. |
| 238 | +
|
| 239 | +**Incorrect:** |
| 240 | +```bash |
| 241 | +for file in /home/user/docs |
| 242 | +do |
| 243 | + echo Processing $file |
| 244 | +done |
| 245 | +``` |
| 246 | +
|
| 247 | +### Output: |
| 248 | +```bash |
| 249 | +Processing /home/user/docs |
| 250 | +``` |
| 251 | +**Correct:** |
| 252 | +```bash |
| 253 | +for file in /home/user/docs/*; do |
| 254 | + echo "Processing $file" |
| 255 | +done |
| 256 | +``` |
| 257 | +
|
| 258 | +**Explanation:** Adding `/*` ensures the loop iterates over each file inside the directory, rather than the directory itself. |
| 259 | +
|
| 260 | +## Debugging Tips |
| 261 | +
|
| 262 | +- `set -x` – Display each command before execution. |
| 263 | +
|
| 264 | +- `$?` – Check the exit status of the last command. |
| 265 | +
|
| 266 | +- `bash -n script.sh` – Validate syntax without running the script. |
| 267 | +
|
| 268 | +- `bash -v script.sh` – Display commands as they execute. |
| 269 | +
|
| 270 | +- `2> error.log` – Redirect errors to a file. |
| 271 | +
|
| 272 | +- `trap 'echo "Error on line $LINENO"' ERR` – Catch runtime errors with line numbers. |
| 273 | +
|
| 274 | +## Key Takeaways |
| 275 | +
|
| 276 | +Most common Bash errors result from: |
| 277 | +
|
| 278 | +- Missing permissions |
| 279 | +
|
| 280 | +- Syntax mistakes |
| 281 | +
|
| 282 | +- Unset variables |
| 283 | +
|
| 284 | +- Running scripts with the wrong shell |
| 285 | +
|
| 286 | +Understanding these errors and using debugging techniques will help you write reliable and maintainable Bash scripts. |
0 commit comments