0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 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.