Creating a script file (.sh) in Bash Scripting - Performance & Efficiency
When we create a script file in bash, it's important to know how the time to create it grows as the script content grows.
We want to understand how the script creation time changes when we add more lines or commands.
Analyze the time complexity of the following code snippet.
#!/bin/bash
echo "#!/bin/bash" > myscript.sh
for i in $(seq 1 100); do
echo "echo Line $i" >> myscript.sh
done
chmod +x myscript.sh
This script creates a new bash script file named myscript.sh and writes 100 lines into it, then makes it executable.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop that writes each line to the file. - How many times: It runs once for each line, here 100 times.
As the number of lines to write increases, the time to create the script grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 write commands |
| 100 | About 100 write commands |
| 1000 | About 1000 write commands |
Pattern observation: The time grows directly with the number of lines added.
Time Complexity: O(n)
This means the time to create the script grows linearly with the number of lines you write.
[X] Wrong: "Adding more lines won't affect the script creation time much because it's just writing text."
[OK] Correct: Each line requires a write operation, so more lines mean more work and more time.
Understanding how loops affect script creation time helps you think clearly about how your scripts scale as they grow.
"What if we used a single echo command with all lines combined instead of a loop? How would the time complexity change?"
Practice
.sh script file in bash?Solution
Step 1: Understand what a script file is
A.shfile contains bash commands saved to run multiple times without typing again.Step 2: Identify the purpose of saving commands
Saving commands helps automate tasks and avoid retyping, which matches To save a list of commands to run them later easily.Final Answer:
To save a list of commands to run them later easily -> Option CQuick Check:
Script file purpose = save commands [OK]
- Thinking .sh files are for text or images
- Confusing scripts with compiled programs
- Not knowing scripts automate commands
Solution
Step 1: Identify the correct shebang for bash scripts
The shebang line tells the system which program runs the script. For bash, it is#!/bin/bash.Step 2: Eliminate incorrect interpreters
Options A, C, and D point to other interpreters or invalid commands.Final Answer:
#!/bin/bash -> Option BQuick Check:
Shebang for bash = #!/bin/bash [OK]
- Using wrong interpreter path
- Forgetting the #! at the start
- Confusing bash with other languages
greet.sh?#!/bin/bash echo "Hello, $USER!"
Solution
Step 1: Understand the variable $USER in bash
$USER is an environment variable that holds the current user's username.Step 2: Predict the echo output
The echo command prints "Hello, " followed by the username, so it outputs "Hello, your-username!" where your-username is the actual user.Final Answer:
Hello, your-username! -> Option DQuick Check:
$USER expands to username = Hello, your-username! [OK]
- Thinking $USER prints literally
- Expecting 'root' always
- Confusing syntax causing errors
myscript.sh but when you run ./myscript.sh, it says Permission denied. What is the likely fix?Solution
Step 1: Understand the cause of 'Permission denied'
This error usually means the file is not executable by the user.Step 2: Make the script executable
Usingchmod +x myscript.shadds execute permission, allowing./myscript.shto run.Final Answer:
Run chmod +x myscript.sh to make it executable -> Option AQuick Check:
Permission denied fix = chmod +x [OK]
- Forgetting to make script executable
- Only adding shebang without permissions
- Renaming file instead of fixing permissions
backup.sh that prints "Backup started" and then lists files in the current directory. Which script content is correct?Solution
Step 1: Check the shebang and commands order
The script must start with#!/bin/bashto run in bash, then commands follow in order.Step 2: Verify commands and syntax
#!/bin/bash echo "Backup started" ls has correct shebang, echo with quotes, andlscommand to list files. Others have wrong order, missing quotes, or invalid commands.Final Answer:
#!/bin/bash echo "Backup started" ls -> Option AQuick Check:
Correct script structure = #!/bin/bash echo "Backup started" ls [OK]
- Placing shebang after commands
- Using wrong commands like 'list'
- Missing quotes around strings
Ready to prove it?
Pass the test and this pattern moves unknown
