Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
Question 1 of 60 · Structure of Shell Scripts · Easy
Which line is conventionally placed at the very top of a shell script to specify the interpreter?
Not quite. Try another option.
Answer: #!/bin/bash
The shebang line #!/bin/bash tells the system which interpreter should execute the script.
Question 2 of 60 · Creating and Executing Scripts · Easy
Which command makes a shell script named run.sh executable?
Not quite. Try another option.
Answer: chmod +x run.sh
chmod +x adds execute permission to the file so it can be run directly.
Question 3 of 60 · while loop · Easy
In a shell while loop, the loop body executes as long as the condition is:
Not quite. Try another option.
Answer: True
A while loop repeats its body while the tested condition remains true.
Question 4 of 60 · for loop · Easy
What is the correct keyword used to end a for loop in shell scripting?
Not quite. Try another option.
Answer: done
Both for and while loops in shell scripts are terminated with the done keyword.
Question 5 of 60 · functions · Easy
Which is a valid way to define a function named greet in bash?
Not quite. Try another option.
Answer: greet() { echo hi; }
In bash, functions are defined as name() { commands; }.
Question 6 of 60 · arrays · Easy
How do you access the first element of a bash array named arr?
Not quite. Try another option.
Answer: ${arr[0]}
Bash arrays are zero-indexed, so the first element is ${arr[0]}.
Question 7 of 60 · Command-Line Arguments · Easy
Inside a shell script, which variable holds the first command-line argument?
Not quite. Try another option.
Answer: $1
$1 refers to the first argument; $0 is the script name.
Question 8 of 60 · Command-Line Arguments · Easy
Which special variable gives the total number of command-line arguments passed to a script?
Not quite. Try another option.
Answer: $#
$# stores the count of positional parameters passed to the script.
Question 9 of 60 · Interactive Shell Scripts · Easy
Which command is used to read input from the user into a variable in a shell script?
Not quite. Try another option.
Answer: read
The read command captures user input from standard input into one or more variables.
Question 10 of 60 · if · Easy
Which keyword marks the end of an if statement in shell scripting?
Not quite. Try another option.
Answer: fi
An if block in shell is closed with fi, which is if spelled backwards.
Question 11 of 60 · if-else · Easy
Which keyword introduces the alternative block in a shell if-else statement?
Not quite. Try another option.
Answer: else
else provides the code that runs when the if condition is false.
Question 12 of 60 · nested if · Easy
What best describes a nested if in shell scripting?
Not quite. Try another option.
Answer: An if statement placed inside another if block
A nested if is an if statement contained within the body of another if.
Question 13 of 60 · test command and [] · Easy
In shell scripting, [ ] is equivalent to which command?
Not quite. Try another option.
Answer: test
The [ is essentially another name for the test command used to evaluate conditions.
Question 14 of 60 · test command and [] · Easy
Which operator checks whether two integers are equal inside [ ]?
Not quite. Try another option.
Answer: -eq
-eq tests numeric equality of two integers within the test command.
Question 15 of 60 · case Statement · Easy
Which keyword ends a case statement in shell scripting?
Not quite. Try another option.
Answer: esac
A case block is terminated by esac, which is case spelled backwards.
Question 16 of 60 · Operators in Shell Scripting · Easy
Which operator is used for string equality comparison in [ ]?
Not quite. Try another option.
Answer: =
The = operator compares two strings for equality inside the test command.
Question 17 of 60 · Operators in Shell Scripting · Easy
Which logical operator represents AND in shell conditional expressions?
Not quite. Try another option.
Answer: &&
&& is the logical AND operator; the right side runs only if the left succeeds.
Question 18 of 60 · arrays · Easy
Which syntax correctly declares an array in bash with three values?
Not quite. Try another option.
Answer: arr=(one two three)
Bash arrays are created with values inside parentheses: arr=(one two three).
Question 19 of 60 · functions · Easy
How is a value typically returned as output from a shell function to be captured by the caller?
Not quite. Try another option.
Answer: Using echo and command substitution
Shell functions commonly echo a result which the caller captures via command substitution like $(func).
Question 20 of 60 · while loop · Easy
Which keyword pair correctly begins and ends the body of a while loop in shell?
Not quite. Try another option.
Answer: do ... done
A while loop body is enclosed between do and done.
Question 21 of 60 · while loop · Medium
What will be the output of the following shell script?
bash
i=1
while [ $i -le 3 ]
do
echo -n "$i "
i=$((i+1))
done
Not quite. Try another option.
Answer: 1 2 3
The loop runs while $i is less than or equal to 3, printing 1, 2, and 3. When i becomes 4 the condition fails and the loop stops.
Question 22 of 60 · for loop · Medium
Which for loop correctly iterates over the numbers 1 to 5 using C-style syntax in Bash?
Not quite. Try another option.
Answer: for ((i=1; i<=5; i++)); do echo $i; done
Bash supports C-style loops using double parentheses (( )). The other forms have invalid syntax for Bash.
Question 23 of 60 · functions · Medium
In the script below, what does $1 refer to inside the function?
bash
greet() {
echo "Hello $1"
}
greet World
Not quite. Try another option.
Answer: The first argument passed to the function
Inside a function, positional parameters like $1 refer to arguments passed to the function itself, not to the script. Here $1 is World.
Question 24 of 60 · arrays · Medium
Given arr=(apple banana cherry), what does ${#arr[@]} return?
Not quite. Try another option.
Answer: 3
The syntax ${#arr[@]} returns the number of elements in the array, which is 3. To get the length of a single element you would use ${#arr[0]}.
Question 25 of 60 · arrays · Medium
How do you access the second element of a Bash array named colors?
Not quite. Try another option.
Answer: ${colors[1]}
Bash arrays are zero-indexed, so the second element is at index 1, accessed as ${colors[1]}. The braces are required for correct expansion.
Question 26 of 60 · Structure of Shell Scripts · Medium
What is the purpose of the line #!/bin/bash at the top of a shell script?
Not quite. Try another option.
Answer: It specifies the interpreter used to execute the script
The shebang line #!/bin/bash tells the operating system which interpreter should run the script. Although it starts with #, it is treated specially when it is the first line.
Question 27 of 60 · Creating and Executing Scripts · Medium
Which command makes a script file run.sh executable so it can be run with ./run.sh?
Not quite. Try another option.
Answer: chmod +x run.sh
The chmod +x command adds execute permission to the file, allowing it to be run directly. +r only grants read permission.
Question 28 of 60 · Interactive Shell Scripts · Medium
Which command reads a line of input from the user and stores it in a variable named name?
Not quite. Try another option.
Answer: read name
The read built-in command reads a line from standard input and assigns it to the given variable. The other commands are not valid for reading user input in Bash.
Question 29 of 60 · Command-Line Arguments · Medium
If a script is run as ./script.sh a b c, what value does $# hold?
Not quite. Try another option.
Answer: 3
$# gives the number of positional arguments passed to the script, which is 3 here. The script name itself ($0) is not counted.
Question 30 of 60 · Command-Line Arguments · Medium
What does $@ represent in a shell script?
Not quite. Try another option.
Answer: All command-line arguments as separate words
$@ expands to all positional parameters, each treated as a separate word when quoted. $# gives the count and $? gives the exit status.
Question 31 of 60 · Operators in Shell Scripting · Medium
Which operator is used to test whether two integers are equal in a test/[ ] expression?
Not quite. Try another option.
Answer: -eq
For integer comparison inside [ ], Bash uses -eq for equality. The == and = operators are used for string comparison.
Question 32 of 60 · Operators in Shell Scripting · Medium
What is the result of the arithmetic expansion echo $((7 % 3))?
Not quite. Try another option.
Answer: 1
The % operator returns the remainder of division. remainder , so the output is 1.
Question 33 of 60 · if · Medium
What will the following script print?
bash
x=10
if [ $x -gt 5 ]
then
echo "big"
fi
Not quite. Try another option.
Answer: big
Since $x (10) is greater than 5, the -gt test is true and the echo inside the if block executes, printing big.
Question 34 of 60 · if-else · Medium
Consider the script below. What is printed when the user enters 4?
bash
read n
if [ $((n % 2)) -eq 0 ]
then
echo "even"
else
echo "odd"
fi
Not quite. Try another option.
Answer: even
4 % 2 equals 0, so the -eq 0 condition is true and the script prints even.
Question 35 of 60 · nested if · Medium
In a nested if structure, when is the innermost block executed?
Not quite. Try another option.
Answer: Only when all enclosing outer conditions are also true
A nested if is evaluated only if its surrounding if conditions are true, since control must reach the inner block first.
Question 36 of 60 · test command and [ ] · Medium
Which expression correctly checks whether the file data.txt exists and is a regular file?
Not quite. Try another option.
Answer: [ -f data.txt ]
The -f operator tests whether a path exists and is a regular file. -d checks for a directory and -x checks execute permission.
Question 37 of 60 · test command and [ ] · Medium
Why is it important to put spaces inside the brackets, as in [ $a -lt $b ]?
Not quite. Try another option.
Answer: Because [ is a command and requires its arguments to be separated by spaces
[ is actually a synonym for the test command, so the shell needs whitespace to separate [, the operands, and the closing ] as distinct arguments.
Question 38 of 60 · case Statement · Medium
What will the following script output when fruit=banana?
bash
case $fruit in
apple) echo "A";;
banana) echo "B";;
*) echo "other";;
esac
Not quite. Try another option.
Answer: B
The value banana matches the banana) pattern, so B is printed. The *) default only runs when no other pattern matches.
Question 39 of 60 · case Statement · Medium
In a case statement, what role does the *) pattern serve?
Not quite. Try another option.
Answer: It acts as a default case that matches any value not matched earlier
The * pattern matches any string, so placing *) last makes it a catch-all default, similar to default in other languages.
Question 40 of 60 · functions · Medium
How can a shell function return an exit status that the caller can check with $??
Not quite. Try another option.
Answer: Using the return statement with a numeric value between 0 and 255
return N sets the function's exit status, which the caller reads via $?. Note that exit would terminate the whole script, not just the function.
Question 41 of 60 · Command-Line Arguments · Hard
In a Bash script, what is the difference between $@ and $* when both are used unquoted in a for loop iterating over arguments containing spaces?
Not quite. Try another option.
Answer: When unquoted, both $@ and $* behave identically, undergoing word splitting on IFS
The distinction between $@ and $* only matters when quoted. "$@" preserves each argument as a separate word, while "$*" joins them into one string. Unquoted, both undergo identical word splitting on IFS.
Question 42 of 60 · test command and [] · Hard
What is the output of the following?
bash
x=""
if [ $x = "a" ]; then echo yes; else echo no; fi
Not quite. Try another option.
Answer: A syntax error: [: =: unary operator expected
Since $x is empty and unquoted, the expression expands to [ = "a" ], leaving = with no left operand. test then reports unary operator expected. Quoting as [ "$x" = "a" ] would fix it and print no.
Question 43 of 60 · arrays · Hard
Given arr=(10 20 30 40), what does ${arr[@]:1:2} evaluate to?
Not quite. Try another option.
Answer: 20 30
Array slicing ${arr[@]:start:length} returns length elements beginning at index start. Starting at index 1 (20) and taking 2 elements yields 20 30.
Question 44 of 60 · Operators in Shell Scripting · Hard
What is the result of echo $(( 5 & 3 | 2 )) in Bash arithmetic?
Not quite. Try another option.
Answer: 3
Bitwise AND binds tighter than OR. 5 & 3 = 0101 & 0011 = 0001 (1). Then 1 | 2 = 0001 | 0010 = 0011 = 3.
Question 45 of 60 · for loop · Hard
What does this C-style loop print?
bash
for ((i=0; i<3; i++)); do
echo -n "$i"
done
Not quite. Try another option.
Answer: 012
The C-style for runs for i = 0, 1, 2 (stops when i<3 is false). echo -n suppresses newlines, so output is 012 with no separators.
Question 46 of 60 · functions · Hard
In the following, what does the script print?
bash
f() { local x=5; g; }
g() { echo $x; }
x=99
f
Not quite. Try another option.
Answer: 5
Bash uses dynamic scoping for local variables. When g runs inside f, x is still locally set to 5 within the call stack, so g sees 5, not the global 99.
Question 47 of 60 · while loop · Hard
Why might the following pipeline print 0 instead of the expected line count?
bash
count=0
cat file.txt | while read line; do
count=$((count+1))
done
echo $count
Not quite. Try another option.
Answer: The while runs in a subshell, so count changes don't persist to the parent
In a pipeline, each command runs in a subshell. The while loop's modifications to count are lost when the subshell exits. Using while read line; do ...; done < file.txt avoids the subshell.
Question 48 of 60 · case Statement · Hard
Given x="apple", which pattern in a case statement matches?
bash
case $x in
ae) echo one;;
[aA]) echo two;;
*) echo three;;
esac
Not quite. Try another option.
Answer: Prints one
case tests patterns top to bottom and stops at the first match. a*e matches apple (starts with a, ends with e), so one prints and later patterns are never tested.
Question 49 of 60 · if-else · Hard
What does the exit status of the if condition depend on here?
bash
if grep -q "foo" file.txt; then echo found; else echo missing; fi
Not quite. Try another option.
Answer: The exit status of grep, where 0 means match found
if evaluates the exit status of the command. grep -q returns 0 (true) when a match is found, triggering the then branch, and non-zero otherwise.
Question 50 of 60 · nested if · Hard
What is printed?
bash
n=15
if (( n % 3 == 0 )); then
if (( n % 5 == 0 )); then echo FizzBuzz
else echo Fizz; fi
elif (( n % 5 == 0 )); then echo Buzz
fi
Not quite. Try another option.
Answer: FizzBuzz
15 % 3 == 0 is true, entering the outer block. Inside, 15 % 5 == 0 is also true, so the inner then executes, printing FizzBuzz.
Question 51 of 60 · Structure of Shell Scripts · Hard
Why is the shebang #!/usr/bin/env bash often preferred over #!/bin/bash?
Not quite. Try another option.
Answer: It locates bash via PATH, improving portability across systems
/usr/bin/env bash searches PATH for bash, so it works even when bash is installed at a non-standard location (e.g. /usr/local/bin on some systems), unlike a hardcoded /bin/bash.
Question 52 of 60 · arrays · Hard
Given declare -A m; m[x]=1; m[y]=2, what does echo ${#m[@]} output?
Not quite. Try another option.
Answer: 2
declare -A creates an associative array. ${#m[@]} returns the number of elements (key-value pairs), which is 2.
Question 53 of 60 · Operators in Shell Scripting · Hard
What does [[ "file.txt" == *.txt ]] && echo match output, and why?
Not quite. Try another option.
Answer: match, because [[ ]] performs pattern matching with unquoted *.txt
Inside [[ ]], the right side of == is treated as a glob pattern when unquoted. *.txt matches file.txt, so the command succeeds and prints match. Quoting the pattern would force literal comparison.
Question 54 of 60 · Command-Line Arguments · Hard
After running ./script.sh a b c with shift 2 at the top, what does echo $1 $# print?
Not quite. Try another option.
Answer: c 1
shift 2 removes the first two positional parameters (a, b). $1 becomes c and $# (argument count) becomes 1.
Question 55 of 60 · Interactive Shell Scripts · Hard
What is the effect of read -r -t 5 -p "Name: " name if the user types nothing for 6 seconds?
Not quite. Try another option.
Answer: read returns non-zero after 5s, leaving name unset/empty
-t 5 sets a 5-second timeout. If no input completes in that time, read returns a non-zero status (>128) and name is left empty. -r prevents backslash interpretation and -p shows the prompt.
Question 56 of 60 · Creating and Executing Scripts · Hard
A script runs correctly with bash script.sh but fails with ./script.sh reporting Permission denied. What is the most likely cause?
Not quite. Try another option.
Answer: The execute permission bit is not set on the file
bash script.sh invokes the interpreter directly and needs only read permission. ./script.sh requires the execute bit (chmod +x script.sh). Without it, the kernel refuses to run the file, giving Permission denied.
Question 57 of 60 · functions · Hard
What does this script output?
bash
compute() { return 7; }
compute
echo $?
Not quite. Try another option.
Answer: 7
A shell function's return sets the exit status ($?), not a value passed back like in other languages. return 7 makes $? equal to 7. To return data, functions typically echo and use command substitution.
Question 58 of 60 · while loop · Hard
What does the following print?
bash
i=5
while (( i-- )); do
echo -n "$i "
done
Not quite. Try another option.
Answer: 4 3 2 1 0
(( i-- )) uses the value of i (post-decrement) as the truth test, then decrements. The loop continues while the value is non-zero. Bodies run for original i=5,4,3,2,1, but echo shows i after decrement: 4 3 2 1 0. When i is 1, test is true then i becomes 0 and prints 0; next test on 0 is false.
Question 59 of 60 · test command and [] · Hard
Which comparison correctly tests whether the integer in $n is greater than 10 in a POSIX [ ] test?
Not quite. Try another option.
Answer: [ "$n" -gt 10 ]
In [ ], integer comparisons use -gt, -lt, -eq, etc. > is a string/redirection operator (and would redirect to a file named 10 here), while gt without a dash is invalid.
Question 60 of 60 · if · Hard
What is the behavior of if [ -n "$var" ] && [ -f "$var" ]; then echo ok; fi?
Not quite. Try another option.
Answer: Prints ok only if $var is non-empty and names an existing regular file
&& short-circuits: both tests must succeed. -n "$var" checks the string is non-empty and -f "$var" checks it is an existing regular file. Only when both are true does echo ok run.
That's unit 10
Answer a few questions and your score shows up here.
Nearly through. Did unit 10 of CSE105 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 10 of CSE105 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.