What if you could make your computer do your daily chores with just one click?
Why Running scripts in Bash Scripting? - Purpose & Use Cases
Imagine you have a list of tasks to do every day on your computer, like cleaning up files or checking system status. Doing each step by hand means opening programs, typing commands one by one, and repeating this every day.
This manual way is slow and boring. You might forget a step or make a typo. It wastes your time and can cause mistakes that are hard to fix.
Running scripts lets you bundle all these commands into one file. You just run the script once, and it does all the tasks automatically, fast and without errors.
rm temp1.txt
echo 'Done cleaning'
ls -l./cleanup.sh
Running scripts frees you from repetitive work and lets your computer do the boring tasks for you.
System administrators run scripts every night to back up data and update software without lifting a finger.
Manual commands are slow and error-prone.
Scripts automate tasks with one simple command.
Running scripts saves time and reduces mistakes.
Practice
myscript.sh after making it executable?Solution
Step 1: Understand script execution permission
Before running a script with./myscript.sh, it must have execute permission set usingchmod +x myscript.sh.Step 2: Run the script using relative path
Using./myscript.shruns the script in the current directory. Other commands likerun,execute, orstartare not valid bash commands.Final Answer:
Use./myscript.shto run the script. -> Option AQuick Check:
Executable script run with./script.sh= A [OK]
- Trying to run script without execute permission
- Using invalid commands like run or start
- Not specifying path to script
backup.sh without changing its permissions?Solution
Step 1: Understand running scripts without execute permission
If the script is not executable, you can run it by explicitly calling the shell interpreter likebash backup.sh.Step 2: Identify correct syntax
bash backup.shruns the script using bash without needing execute permission. Other options either require execute permission or are invalid commands.Final Answer:
bash backup.sh-> Option CQuick Check:
Run script with interpreter command = B [OK]
- Trying to run with ./ without execute permission
- Using invalid commands like execute
- Adding extra words like 'run' before script name
hello.sh with content:#!/bin/bash echo "Hello, $1!"
What is the output of
./hello.sh World?Solution
Step 1: Understand script parameter usage
The script uses$1which is the first argument passed to the script. Here,Worldis passed as the first argument.Step 2: Predict output after substitution
The echo command prints "Hello, World!" by replacing$1with the argument.Final Answer:
Hello, World! -> Option BQuick Check:
Argument $1 replaced by 'World' = A [OK]
- Printing literal $1 instead of argument
- Missing argument causing empty output
- Assuming syntax error due to shebang
./myscript.sh but get Permission denied. What is the most likely fix?Solution
Step 1: Identify cause of 'Permission denied'
This error usually means the script file does not have execute permission.Step 2: Fix permission issue
Usechmod +x myscript.shto add execute permission, allowing the script to run.Final Answer:
Add execute permission withchmod +x myscript.sh. -> Option AQuick Check:
Permission denied fixed by chmod +x = D [OK]
- Trying to run script again without changing permissions
- Renaming script does not fix permissions
- Deleting script is unnecessary
deploy.sh located in /home/user/scripts from anywhere in the terminal without typing the full path. What is the best way to do this?Solution
Step 1: Understand PATH environment variable
PATH lists directories where the shell looks for commands. Adding a directory lets you run scripts inside it by name.Step 2: Add script directory to PATH
By adding/home/user/scriptsto PATH, you can rundeploy.shfrom anywhere simply by typingdeploy.sh.Final Answer:
Add/home/user/scriptsto the PATH environment variable. -> Option DQuick Check:
PATH addition enables running scripts anywhere = C [OK]
- Copying script to /tmp is temporary and not recommended
- Running with full path every time is inconvenient
- Renaming script does not affect running from anywhere
Ready to prove it?
Pass the test and this pattern moves unknown
