0
0
Bash Scriptingscripting~10 mins

Creating a script file (.sh) in Bash Scripting - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating a script file (.sh)
Open text editor
Write script lines
Save file with .sh extension
Make file executable
Run script in terminal
See output or result
This flow shows how to create a shell script file, make it executable, and run it to see the output.
Execution Sample
Bash Scripting
#!/bin/bash

echo "Hello, world!"
This script prints 'Hello, world!' to the terminal when run.
Execution Table
StepActionCommand/CodeResult/Output
1Open text editornano hello.shText editor opens with new file
2Write script lines#!/bin/bash echo "Hello, world!"Script content written in editor
3Save fileCtrl+O then EnterFile 'hello.sh' saved
4Exit editorCtrl+XBack to terminal
5Make file executablechmod +x hello.shFile permission changed to executable
6Run script./hello.shHello, world!
7EndScript finished runningTerminal prompt returns
💡 Script runs once and ends after printing the message.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 6
script_filenonehello.sh content written in editorhello.sh marked executablescript executed, output shown
Key Moments - 3 Insights
Why do we add #!/bin/bash at the top of the script?
This line tells the system to use the bash shell to run the script, as shown in execution_table step 2.
Why do we need to run chmod +x before running the script?
chmod +x makes the script file executable, allowing the system to run it, as seen in execution_table step 5.
What happens if we try to run the script without ./ before the filename?
The system may not find the script because current directory is not in PATH; using ./hello.sh runs it explicitly, shown in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what command makes the script file executable?
A./hello.sh
Bchmod +x hello.sh
Cnano hello.sh
Decho "Hello, world!"
💡 Hint
Check step 5 in the execution_table where file permissions change.
At which step does the script actually run and print output?
AStep 2
BStep 4
CStep 6
DStep 1
💡 Hint
Look for the step where './hello.sh' is executed and output appears.
If you forget to add #!/bin/bash at the top, what might happen?
AScript may run with a different shell or error
BScript runs normally without any issue
Cchmod +x will fail
DFile won't save
💡 Hint
Refer to key_moments about the purpose of the shebang line.
Concept Snapshot
#!/bin/bash at top tells system to use bash
Write commands in plain text editor
Save file with .sh extension
Make executable with chmod +x
Run script with ./filename.sh
Script runs and shows output in terminal
Full Transcript
To create a shell script file, first open a text editor like nano. Write your script lines, starting with #!/bin/bash to specify the shell. Save the file with a .sh extension, for example hello.sh. Exit the editor and make the file executable by running chmod +x hello.sh in the terminal. Finally, run the script by typing ./hello.sh. The script will execute and print its output, such as 'Hello, world!'. This process ensures your script runs correctly in the bash shell.