Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Running scripts in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Running scripts
Write script file
↓
Make script executable
↓
Run script with ./ or bash
↓
Script runs commands
↓
Output shown in terminal
↓
Script ends
This flow shows how you write a script, make it executable, run it, and see the output in the terminal.
Execution Sample
Bash Scripting
#!/bin/bash
 echo "Hello, world!"
This script prints 'Hello, world!' when run.
Execution Table
StepActionCommand/CodeResult/Output
1Create script fileecho -e '#!/bin/bash\n echo "Hello, world!"' > hello.shFile 'hello.sh' created with script content
2Make script executablechmod +x hello.shScript 'hello.sh' is now executable
3Run script./hello.shHello, world!
4Script endsScript finishes runningTerminal prompt returns
💡 Script finishes after printing output and terminal prompt returns
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
hello.sh fileDoes not existCreated with script contentExists and executableExecuted and output shownExists and executable
Key Moments - 3 Insights
Why do we need to make the script executable before running it?
The script must have execute permission to run with './'. Without it, the system will refuse to run the file. See execution_table step 2 and 3.
Can we run the script without making it executable?
Yes, by running 'bash hello.sh' you don't need execute permission. This runs the script with the bash interpreter directly. See execution_table step 3 alternative.
What does the '#!/bin/bash' line do?
It tells the system to use bash to run the script. This is called a shebang. Without it, the system might use a different shell.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when running the script at step 3?
AHello, world!
BScript not found
CPermission denied
DNo output
💡 Hint
Check the 'Result/Output' column at step 3 in the execution_table
At which step is the script made executable?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table for making executable
If we skip step 2 and try to run './hello.sh', what will likely happen?
AScript runs normally
BPermission denied error
CFile not found error
DScript prints nothing
💡 Hint
Refer to key_moments about execute permission and step 2 in execution_table
Concept Snapshot
Running scripts in bash:
1. Write your script in a file (e.g., hello.sh).
2. Make it executable with 'chmod +x filename'.
3. Run it with './filename' or 'bash filename'.
4. The script runs commands and shows output.
5. The shebang '#!/bin/bash' tells which shell to use.
Full Transcript
To run a bash script, first create a file with your commands. For example, a file named hello.sh with the content '#!/bin/bash' and 'echo "Hello, world!"'. Next, make the script executable using 'chmod +x hello.sh'. This allows you to run it directly with './hello.sh'. When you run it, the script executes the commands inside and prints 'Hello, world!' to the terminal. The script ends and the terminal prompt returns. Alternatively, you can run the script without making it executable by using 'bash hello.sh'. The first line '#!/bin/bash' tells the system to use bash to run the script. Without execute permission, running './hello.sh' will give a permission denied error.

Practice

(1/5)
1. What is the correct way to run a bash script named myscript.sh after making it executable?
easy
A. Use ./myscript.sh to run the script.
B. Use run myscript.sh to run the script.
C. Use execute myscript.sh to run the script.
D. Use start myscript.sh to run the script.

Solution

  1. Step 1: Understand script execution permission

    Before running a script with ./myscript.sh, it must have execute permission set using chmod +x myscript.sh.
  2. Step 2: Run the script using relative path

    Using ./myscript.sh runs the script in the current directory. Other commands like run, execute, or start are not valid bash commands.
  3. Final Answer:

    Use ./myscript.sh to run the script. -> Option A
  4. Quick Check:

    Executable script run with ./script.sh = A [OK]
Hint: Make script executable, then run with ./script.sh [OK]
Common Mistakes:
  • Trying to run script without execute permission
  • Using invalid commands like run or start
  • Not specifying path to script
2. Which of the following is the correct syntax to run a bash script named backup.sh without changing its permissions?
easy
A. ./backup.sh
B. sh run backup.sh
C. bash backup.sh
D. execute backup.sh

Solution

  1. Step 1: Understand running scripts without execute permission

    If the script is not executable, you can run it by explicitly calling the shell interpreter like bash backup.sh.
  2. Step 2: Identify correct syntax

    bash backup.sh runs the script using bash without needing execute permission. Other options either require execute permission or are invalid commands.
  3. Final Answer:

    bash backup.sh -> Option C
  4. Quick Check:

    Run script with interpreter command = B [OK]
Hint: Use bash script.sh to run without execute permission [OK]
Common Mistakes:
  • Trying to run with ./ without execute permission
  • Using invalid commands like execute
  • Adding extra words like 'run' before script name
3. Given the script hello.sh with content:
#!/bin/bash
echo "Hello, $1!"

What is the output of ./hello.sh World?
medium
A. Hello, $1!
B. Hello, World!
C. Hello, !
D. Syntax error

Solution

  1. Step 1: Understand script parameter usage

    The script uses $1 which is the first argument passed to the script. Here, World is passed as the first argument.
  2. Step 2: Predict output after substitution

    The echo command prints "Hello, World!" by replacing $1 with the argument.
  3. Final Answer:

    Hello, World! -> Option B
  4. Quick Check:

    Argument $1 replaced by 'World' = A [OK]
Hint: Arguments like $1 print passed values in scripts [OK]
Common Mistakes:
  • Printing literal $1 instead of argument
  • Missing argument causing empty output
  • Assuming syntax error due to shebang
4. You try to run a script with ./myscript.sh but get Permission denied. What is the most likely fix?
medium
A. Add execute permission with chmod +x myscript.sh.
B. Run the script with ./myscript.sh again.
C. Rename the script to myscript.sh.sh.
D. Delete the script and create a new one.

Solution

  1. Step 1: Identify cause of 'Permission denied'

    This error usually means the script file does not have execute permission.
  2. Step 2: Fix permission issue

    Use chmod +x myscript.sh to add execute permission, allowing the script to run.
  3. Final Answer:

    Add execute permission with chmod +x myscript.sh. -> Option A
  4. Quick Check:

    Permission denied fixed by chmod +x = D [OK]
Hint: Use chmod +x to fix permission denied errors [OK]
Common Mistakes:
  • Trying to run script again without changing permissions
  • Renaming script does not fix permissions
  • Deleting script is unnecessary
5. You want to run a script 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?
hard
A. Run the script with bash /home/user/scripts/deploy.sh every time.
B. Copy deploy.sh to /tmp directory.
C. Rename the script to deploy without extension.
D. Add /home/user/scripts to the PATH environment variable.

Solution

  1. 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.
  2. Step 2: Add script directory to PATH

    By adding /home/user/scripts to PATH, you can run deploy.sh from anywhere simply by typing deploy.sh.
  3. Final Answer:

    Add /home/user/scripts to the PATH environment variable. -> Option D
  4. Quick Check:

    PATH addition enables running scripts anywhere = C [OK]
Hint: Add script folder to PATH to run scripts anywhere [OK]
Common Mistakes:
  • 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard