Commands typed at a prompt disappear from view as you work. A script keeps a small sequence in a text file so you can inspect it, supply different arguments and run the same instructions again.
You will write a five-line Bash script that prints a label and one spaced argument. Run it first by choosing Bash explicitly, then through its own pathname after a narrow permission change. The output will be the same, but the interpreter and access requirements need separate explanations.
1. Distinguish the text, interpreter and argument
Complete Inspect listeners and combine network clues and settle its cleanup. Earlier lessons taught quoting, variables, child environments, printf, nano and narrow permission changes. A script combines those skills in a file; it does not require a new programming environment.
An interpreter reads program text and carries out its instructions.
Here it is the selected guest's /bin/bash. A script argument is data
supplied after the script's filename when you run it. It is separate from
editing a literal inside the script.
| Form | What chooses the interpreter? | Important access distinction |
|---|---|---|
/bin/bash --noprofile --norc show-label.sh 'Delivery desk' | The command explicitly selects /bin/bash and gives it the file to read. | Bash needs to read the script; the script's own execute bit is not required for this form. |
./show-label.sh 'Delivery desk' | Direct pathname execution uses the file's interpreter line on this Linux guest. | The script needs applicable execution access as well as a usable interpreter and pathname. |
The leading ./ means a path in the current directory. It avoids searching
PATH for the script name. Neither invocation changes PATH or a startup file.
Both run a separate Bash process; neither imports the file into the current
interactive shell. Do not source or evaluate this practice file in the
parent shell.
The shebang is the first line #!/bin/bash: #! followed by the selected
interpreter pathname. It must be the actual first line, without Markdown
fences or leading text. Direct execution uses that pathname. When Bash is
explicitly asked to read this same file, the line is a comment to Bash;
the explicit command has already chosen the interpreter.
Execute access is only one condition. Path search permissions, read access, the interpreter's availability and filesystem policy also matter. If the declared environment prevents direct execution, preserve the diagnostic and reconcile the baseline. Do not add sudo, alter mount settings or treat explicit interpretation as permission to bypass a policy.
Exercise: run one saved script two ways
Goal: compare explicit and direct Bash execution, preserve a spaced argument, and verify that the child's variable stays separate from the parent.
Starting state: the declared full Ubuntu guest is ready, previous lesson cleanup is settled, and your ordinary account can create its own fresh practice directory. The complete synthetic script is supplied below; no additional runtime or download is required.
2. Bound the child environment and reserve its one variable name
Use the ordinary nonzero-UID account in the full Ubuntu guest. Run one line at a time, read the result and stop on unexpected setup or file errors. Inspect the taught command origins first:
type cd pwd printf set declare type id ps mkdir ls cat nano stat chmod wc rm rmdir bash
type -a ls
type -P ls
type -P bash
Require expected Bash builtins and guest executables. In particular, printf must be the Bash builtin, not a function that could be exported to a child. For ls, accept only the understood color-only alias and expected executable paths; stop for a hidden function or other origin. The selected /bin/bash must be the normal distribution interpreter, including the declared guest's ordinary /bin-to-/usr/bin layout where applicable. Do not substitute an unknown interpreter to make the exercise run.
Then inspect the familiar guest and option context:
id -u
id -un
ps -p $ -o comm=
ps -p 1 -o comm=
set -o
ls -ld -- /bin/bash
Require the recorded ordinary identity, Bash and systemd PID 1, with the expected interpreter entry. Keep the taught errexit-off and allexport-off baseline. Require nounset and posix off for this introductory Bash setup; preserve a different configuration rather than changing it here.
The script uses only one new course variable name. The taught declare -p
inspects its actual declaration, not merely an exported value:
declare -p VECTA_LAB_SCRIPT_LABEL
declare -p BASH_ENV
Require a named absence diagnostic for each. Any declaration of the course name is a collision, including an empty, readonly or unexported one. Preserve it and stop. BASH_ENV is a separate startup input: a noninteractive Bash can read the file it names. The explicit --noprofile and --norc flags do not suppress BASH_ENV, so keep that absence condition before every run.
Two existing Bash variables need a different check:
declare -p SHELLOPTS BASHOPTS
These variables normally exist and are readonly. Require the ordinary readonly declarations without the export attribute x. The r attribute means readonly, not an error. An exported option list can enable options in a new Bash before startup processing. Preserve an exported or unexpected declaration and reconcile it; do not unset or change these system variables.
The course variable remains absent in the parent. Only the child script will assign it. The script does not export it, edit unrelated values or change the parent's environment. We will check that particular absence after runs rather than claim to audit every possible system setting.
3. Create and inspect the exact script
Own only ~/linux-practice/run-a-small-bash-script/, show-label.sh and the
three named output files introduced below. Check the known parent and new
task entry:
ls -ld ~/linux-practice
ls -ld ~/linux-practice/run-a-small-bash-script
Require an ordinary parent owned by your recorded account and the task name absent. An existing task belongs to the resume branch. After acceptance:
mkdir ~/linux-practice/run-a-small-bash-script
ls -ld ~/linux-practice/run-a-small-bash-script
Accept the new ordinary owned directory, then enter and check separately:
cd ~/linux-practice/run-a-small-bash-script
pwd
ls -a
Require the exact recorded task path and only dot entries. The absent show-label.sh name can now be opened with the already-taught editor:
nano show-label.sh
Enter exactly these five lines, including a final newline. The surrounding Markdown fence is not part of the file:
#!/bin/bash
VECTA_LAB_SCRIPT_LABEL='Workshop'
printf 'Label: %s\n' "$VECTA_LAB_SCRIPT_LABEL"
printf 'First argument: <%s>\n' "$1"
printf 'Argument count: %s\n' "$#"
Use the taught Ctrl+O write-out action, confirm show-label.sh, then Ctrl+X. Saving writes the buffer to disk; leaving text in an unsaved buffer does not supply a runnable file. Reopen only the known file if an edit is needed.
Before any execution, inspect its identity, contents and mode:
ls -ld -- show-label.sh
stat -c 'mode=%a uid=%u gid=%g links=%h file=%n' -- show-label.sh
cat show-label.sh
wc -l -- show-label.sh
The selected stat fields were introduced in the ownership lessons. Record the actual full original mode, UID, GID and link count. Require an ordinary file owned by your recorded user, one hard link, owner read/write, no execute bits and no special bits or additional ACL marker. Keep the previous ordinary-permission baseline; stop on an unknown marker or link. Do not assume the original mode is 644: a different creation mask can produce another ordinary mode. Require exactly the five lines and five newline characters; a count alone is not a contents check.
Explain each statement before running it
The second line is the taught assignment form: no spaces around =, with
a single-quoted literal value. This assignment occurs in the child Bash
that runs the file. The following printf uses that child's variable value.
The new positional parameters hold arguments given to this script:
| Expression | Meaning |
|---|---|
"$1" | The first script argument, expanded as one quoted value. |
"$#" | The number of script arguments; it excludes the script pathname. |
The parent removes the syntactic quotes around 'Delivery desk' while
passing its contents as one argument. The child's "$1" keeps that spaced
value together. Expansion supplies data to printf; the argument is not
evaluated again as shell program text. The %s format and \n newline
were taught earlier. Angle brackets make the first argument's boundaries
visible in the output; they are literal characters in the quoted format.
This first script intentionally has no input validation. Supply the one argument shown in each task. A later lesson will check argument count and return a deliberate error for invalid input; do not mistake this small printer for a defensive script yet.
4. Choose Bash explicitly and verify the result
Predict the label, first-argument boundaries and argument count before running. Inspect the known script again if it changed, and require the output name absent:
ls -a
Accept only the known show-label.sh at the exact task cwd. The taught >
creates the fresh output file; it would truncate an existing file, so do
not run over a collision. The startup flags mean no profile or interactive
rc-file reading; the separately checked BASH_ENV condition still applies.
/bin/bash --noprofile --norc show-label.sh 'Delivery desk' > explicit-output.txt
printf 'explicit Bash run exit: %s\n' "$?"
Require successful execution and no unexpected stderr diagnostic. Save or record the immediate status before another command replaces it. Then inspect:
ls -ld -- explicit-output.txt
cat explicit-output.txt
wc -l -- explicit-output.txt
declare -p VECTA_LAB_SCRIPT_LABEL
Require an ordinary owned single-link output, exactly three lines and three newline characters:
Label: Workshop
First argument: <Delivery desk>
Argument count: 1
Require the course variable still absent in the parent. Its absence is expected, even though the child's output demonstrates that its own value was Workshop. The child assignment does not copy back into the parent. This output checks argument content/count, not merely whether Bash exited.
5. Add only the execution bit needed for direct invocation
The explicit interpreter read the file while its execute bits were absent. Direct execution has different access requirements. Recheck the same ordinary single-link script, exact contents and recorded full mode first:
ls -ld -- show-label.sh
stat -c 'mode=%a uid=%u gid=%g links=%h file=%n' -- show-label.sh
cat show-label.sh
Require unchanged identity and original mode. The taught symbolic
chmod u+x adds only owner execute. Because that bit was verified absent,
the planned inverse u-x can restore it after all tasks. Do not change
group/other access, ownership, startup files or filesystem policy.
chmod u+x -- show-label.sh
stat -c 'mode=%a uid=%u gid=%g links=%h file=%n' -- show-label.sh
ls -a
Require only the owner execute bit added to the recorded mode, unchanged ownership/link count and the direct-output.txt name absent. Then run through the inspected current-directory path:
./show-label.sh 'Delivery desk' > direct-output.txt
printf 'direct script run exit: %s\n' "$?"
Require success before treating the output as a result. On a permission, interpreter or path error, preserve the actual diagnostic; do not repair an unknown cause by adding privileges. Inspect the result and parent:
ls -ld -- direct-output.txt
cat direct-output.txt
wc -l -- direct-output.txt
declare -p VECTA_LAB_SCRIPT_LABEL
Require the same three exact lines/newlines as explicit-output.txt and the parent variable still absent. The successful direct path adds evidence that the shebang interpreter and applicable execution access worked in this declared context. It does not mean every text file or filesystem can be executed in the same way.
Independent task: change the program label and the input separately
Keep both guided output files. Before the hints or answer:
- Recheck the script's owned identity, contents and owner-execute-only mode change. Use nano to change only the assigned literal label from Workshop to Review. Preserve the shebang and all three printf statements.
- Save and inspect the entire script, line count and mode. Explain why editing the literal changes the program file while passing an argument does not edit it.
- Invoke the script directly with the new single spaced argument Evening shift, capturing stdout in a fresh independent-output.txt. Preserve its immediate actual status and any stderr diagnostic.
- Check all three output lines/newlines and that the parent course variable remains absent. Compare with both retained guided outputs.
Success: the independent result reflects the new program label and
one new argument without splitting its space; both original outputs retain
their original three lines. Explain explicit interpreter versus direct
execution, the shebang, $1, $#, and the child/parent boundary. A failed
or blocked run is not completed by copying the expected output into a file.
Graduated hints
- The assignment line controls the label; the command line supplies the argument. These are separate inputs to the result.
- Quote the spaced argument in the parent, and retain the double quotes around the positional expansion in the script. Count arguments, not words.
- Use the known direct path and the new absent output name. Capture
$?immediately, then compare actual contents and the parent declaration.
Check Your Understanding
- Why can explicit Bash read the original script without its execute bit, while direct execution has additional requirements? What selects Bash in each case, and why is adding execute access not a universal repair?
- Why does the quoted Evening shift argument produce
$#equal to 1? What separates argument data from the instructions saved in the script? - The child prints Label: Review, yet the parent course variable is absent. Is that a failed assignment? Explain which shell owns the value.
Answer and Reasoning
The only changed script line is this assignment excerpt:
VECTA_LAB_SCRIPT_LABEL='Review'
It belongs in show-label.sh, not at the parent prompt. The complete file still has the same shebang and three printf statements. After saving, rechecking exact contents, original identity, the owner-execute-only mode change and the absent output name, one complete invocation is:
./show-label.sh 'Evening shift' > independent-output.txt
printf 'independent script run exit: %s\n' "$?"
Require success, then inspect the actual output and unchanged parent:
ls -ld -- independent-output.txt
cat independent-output.txt
wc -l -- independent-output.txt
declare -p VECTA_LAB_SCRIPT_LABEL
cat explicit-output.txt direct-output.txt
The new file must be ordinary, owned and single-linked, with exactly:
Label: Review
First argument: <Evening shift>
Argument count: 1
Require three newline characters as well as those exact lines. Both older files still contain the Workshop/Delivery desk result. The argument's space does not create a second argument because the invocation quotes it and the script preserves the expansion as one value.
The parent variable remains absent. Both runs used a child interpreter; changing the child's variable affected its own output, not the parent. The explicit form selected Bash before reading the file, whereas the direct form relied on the first-line interpreter instruction and applicable execution access. The two results can match even though those starting requirements differ.
Understanding-check feedback
- Explicit /bin/bash is already a running interpreter reading the file; it needs applicable read/path access, not that file's execute bit. Direct execution uses the shebang and applicable execution access. An unavailable interpreter, wrong path or filesystem policy can still block it, so a permission change is not an all-purpose repair.
- The parent treats the quoted spaced text as one argument and removes
the syntactic quotes. The child's quoted
$1preserves that one value;$#counts arguments, excluding the script pathname. printf receives the value as data rather than evaluating it again as shell instructions. - The assignment succeeded in the child running the script. The unchanged parent absence is the expected process boundary, not evidence of failure. Sourcing the file would change that model and is outside this exercise.
Diagnose the actual cause before changing permissions
| Observation | Bounded diagnosis and next step |
|---|---|
| Count 2 or only the first word appears | Compare the invocation's quoting and the script's exact quoted positional expansion. |
| Unexpected label | Read the saved file, not just the editor buffer or an old output file. |
| Direct invocation fails while explicit Bash worked | Inspect the actual path, shebang, applicable mode and filesystem policy; the execute bit is one possible difference, not the only cause. |
| Interpreter/path error | Confirm the exact inspected interpreter and script path; do not replace them with an unknown program. |
| Parent variable appears | Stop: the intended child-only boundary was not preserved. Do not unset an unproven preexisting value or claim the normal result. |
| An output name already exists | Preserve and inspect the recorded stage; do not blindly truncate it with another run. |
| Line count is correct but text differs | Counts are supporting evidence, not a substitute for the complete contents check. |
Restore the recorded mode, then clean up
Complete all independent checks first. Recheck the ordinary parent/task entries, then enter the task and verify its path separately:
ls -ld ~/linux-practice
ls -ld ~/linux-practice/run-a-small-bash-script
Require the known ordinary owned directories before:
cd ~/linux-practice/run-a-small-bash-script
pwd
Require the exact recorded task path before inspecting all four named files:
ls -a
ls -ld -- show-label.sh explicit-output.txt direct-output.txt independent-output.txt
stat -c 'mode=%a uid=%u gid=%g links=%h file=%n' -- show-label.sh
cat show-label.sh explicit-output.txt direct-output.txt independent-output.txt
Require only those recognized ordinary owned single-link files and dot entries. The script must have the reviewed independent contents, with only owner execute added to its original recorded mode. After that comparison:
chmod u-x -- show-label.sh
stat -c 'mode=%a uid=%u gid=%g links=%h file=%n' -- show-label.sh
Require the complete original mode, owner/group and link count restored. Do not guess a numeric mode or remove a different bit to make it look familiar. Establish the actual documented rm confirmation responses. English y/n examples do not justify guessing an unfamiliar affirmative response.
rm -i -- show-label.sh explicit-output.txt direct-output.txt independent-output.txt
Each target has its own decision. Declining one prompt does not cancel later prompts; if stopping, decline every remaining target and preserve what remains. After accepted removals, inspect:
ls -a
Require the task empty, then return and verify:
cd ~/linux-practice
pwd
Require the recorded shared-root path and recheck the task entry:
ls -ld -- run-a-small-bash-script
Only after accepting that ordinary owned empty directory:
rmdir -- run-a-small-bash-script
Retain the shared root and all interpreter, parent-variable, startup and filesystem settings. There is no parent course variable to unset after the successful child-only task.
On resume, inspect actual files before choosing a stage. The script may
still say Workshop or may already say Review; owner execute may be absent,
added or already restored. Compare with the recorded original identity/mode,
not an assumed 644 baseline. Preserve existing outputs and label any
incomplete run. Do not replay > over an output or repeat a permission
change without accepting the current state. If cleanup was partial, remove
only remaining recognized named files after their checks; do not recreate
missing outputs just to make the final inventory match.
Sources and other study views
- S18 — Bash 5.2: script invocation, parameters, startup and child environments
- S19 — GNU Coreutils: file/mode inspection and bounded cleanup
- S25 — Ubuntu Noble ps: selected shell and guest context
- S33 — Ubuntu Noble nano
- S67 — GNU nano 7.2: edit, save and inspect the file
- S97 — Linux execve(2): interpreter-script execution
The Bash and nano sources document the selected 5.2 and 7.2 families; distribution revisions and local configuration can differ. The Coreutils manual is newer than the target 9.4 tools. The Linux interpreter-script reference is the man-pages 6.03 companion; it explains the execution model, not evidence that this particular guest run or its access checks have succeeded. Record those actual results separately from the supplied output.
Revision reconstructs the interpreter and argument model. Quick Reference retrieves the two invocation forms and guards.