Shell Scripting
Shell Scripting
a) x = 5
b) x==5
c) x=5 ✅
d) $x=5
e) set x=5
Explanation: In bash, there must be no spaces around the = operator while assigning values to
variables.
a) #!/bin/bash ✅
b) #bash
c) bash!
d) @!/bin/sh
e) #!/usr/bin/env
Explanation: #!/bin/bash is the shebang line that specifies the Bash interpreter path.
a) run [Link]
b) chmod +x [Link] ✅
c) exec [Link]
d) bash -x [Link]
e) set +x [Link]
a) bash [Link]
b) sh [Link]
c) source [Link] ✅
d) ./[Link]
e) exec ./[Link]
Explanation: source (or .) runs the script in the current shell, keeping variables persistent.
a) echo $0 ✅
b) cat /etc/shell
c) ls -s
d) show shell
e) whoami
a) /bin/bash
b) /bin/ksh
c) /bin/csh
d) /bin/vsh ✅
e) /bin/sh
Explanation: /bin/vsh is not a standard shell; others are valid UNIX shells.
✅
c) $x+$y
d) 10+20
e) Error
Explanation: Variables are expanded but not evaluated arithmetically without expr or $(( )).
✅
a) 10
b) 5*2
c) *2
d) 7
e) Error
a) add
b) calc
c) $(( )) ✅
d) expr[]
e) math()
a) scan
b) read ✅
c) input
d) cin
e) in
Explanation: The read command is used to take input from the user.
Q10. What is the correct way to read input into variable name?
a) name=read
b) read name ✅
c) input name
d) scan name
e) readname
Q11. Which of the following commands shows the exit status of the last
executed command?
a) $!
b) $@
c) $? ✅
d) $#
e) $$
a) 6
✅
b) 7
c) 8
d) ++7
e) Syntax Error
Q13. Which of the following variables stores the process ID of the current
shell?
a) $@
b) $#
c) $$ ✅
d) $?
e) $PID
✅
a) Stops script execution
b) Enables debugging
c) Disables all variables
d) Lists shell variables
e) Clears screen
Explanation: set -x enables debugging mode, printing each command before execution.
a) clear var
b) remove var
c) unset var ✅
d) delete var
e) erase var
a) No difference ✅
b) ${var} gives value, $var gives address
c) $var is slower
d) ${var} creates array
e) $var is deprecated
a) Hello World! ✅
b) Hello var!
c) Hello$var!
d) World!
e) Error
a) echo $*
b) set ✅
c) showvars
d) env
e) vars
a) c = $a$b
b) c=$a$b ✅
c) c=a+b
d) c=${a+b}
e) concat a b
a) 5
b) 10
✅
c) true
d) 0
e) 1
✅
a) It fails to execute
b) Runs using system’s default shell
c) Runs as plain text
d) Asks for interpreter
e) Generates syntax error
a) null
b) 0
c) Empty string ✅
d) undefined
e) NaN
Explanation: Uninitialized variables expand to an empty string by default.
a) -l
b) -n ✅
c) -e
d) -E
e) -s
a) 10 ✅
b) 8
c) 7
d) 12
e) Error
a) To remove a variable
✅
b) To assign a variable
c) To make variable available to child processes
d) To unset variables
e) To hide variable from environment
✅
b) The number of arguments
c) The script name
d) The last argument
e) Exit status
✅
a) Lines in the file
b) Command-line arguments
c) Shell variables
d) Loop counters
e) Function parameters
Explanation: $1, $2, etc., are positional parameters for arguments passed to the script.
a) $@
b) $*
c) $# ✅
d) $?
e) $0
a) $@
b) $* ✅
c) $#
d) $?
e) $0
a) $@ ✅
b) $*
c) $#
d) $?
e) $0
✅
a) 1
b) 0
c) null
d) undefined
e) -1
Explanation: $# shows count of arguments; if none passed, it’s 0.
Explanation: $* joins all arguments separated by the first character of IFS (default space).
a) if $# -gt 1
b) if [ $# -ge 1 ] ✅
c) if $# == 1
d) if [ $# = 0 ]
e) if test $#
✅
a) All arguments as one word
b) Each argument as separate word
c) No expansion
d) Only first argument
e) Error
✅
a) Whether a string is null
b) Return value of a command
c) Loop iteration
d) Shell version
e) Variable scope
Explanation: The if condition tests the exit status (0 = true, nonzero = false).
Q37. Which command is used with if to test file types and comparisons?
a) test ✅
b) check
c) cmp
d) assert
e) verify
a) if (condition)
b) if { condition }
c) if [ condition ]; then ... fi ✅
d) if < condition > fi
e) if { condition; done }
a) -eq
b) -lt
c) == ✅
d) =
e) -eqs
✅
a) hello
b) Match
c) No
d) Error
e) hello hello
a) [ -e [Link] ] ✅
b) [ -x [Link] ]
c) [ -f [Link] ]
d) [ [Link] ]
e) [ -d [Link] ]
a) [ $x == null ]
b) [ -n $x ]
c) [ -z $x ] ✅
d) [ empty $x ]
e) [ $x -eq 0 ]
a) 3
✅
b) 0
c) Divisible
d) Not
e) Error
a) [ -d [Link] ]
b) [ -r [Link] ]
c) [ -f [Link] ] ✅
d) [ -s [Link] ]
e) [ -a [Link] ]
Explanation: Bash uses && between two conditions for AND logic.
a) if [ cond1 || cond2 ]
b) if [ cond1 -or cond2 ]
c) if [ cond1 ] || [ cond2 ] ✅
d) if cond1 or cond2
e) if {cond1 || cond2}
✅
a) High
b) Low
c) Error
d) Nothing
e) 10
a) [ -r [Link] ] ✅
b) [ -p [Link] ]
c) [ -s [Link] ]
d) [ -x [Link] ]
e) [ -a [Link] ]
a) Else-if condition ✅
b) Else loop
c) End of file
d) Else final
e) End of if
Explanation: Bash uses for var in list; do … done syntax. {1..10} generates a
sequence.
a) 123
b) 1 2 3
c)
1
2
3
``` ✅
d) [1,2,3]
e) Error
---
a) for (i=0;i<5;i++)
b) for ((i=0;i<5;i++)) ✅
c) loop i=0 to 5
d) while (i<5)
e) foreach i in range(5)
Q56. Which loop is used when the number of iterations is not known?
a) for
b) until
c) while ✅
d) repeat
e) foreach
✅
a) Infinite loop
b) Prints Hello 1 Hello 2 Hello 3
c) Syntax error
d) Prints 1, 2, 3 only
e) Prints 4 times
a) while
✅
b) for
c) until
d) do-while
e) if
Explanation: The until loop runs until its test condition is true (reverse of while).
✅
a) 1 2
b) 1 2 3
c) Infinite loop
d) 1 2 3 4
e) None
Q60. What will happen if you forget done at the end of a loop?
✅
a) Script exits
b) Syntax error
c) Runs forever
d) Ignores loop
e) Prints nothing
Explanation: Missing done causes a syntax error since it ends the loop block.
✅
a) 1 2 3 4
b) 1 2
c) 1 2 3
d) 3 4
e) None
Explanation: break stops the loop when i==3, printing only 1 and 2.
Q62. What is the output of:
for i in 1 2 3 4
do
[ $i -eq 2 ] && continue
echo $i
done
a) 1 3 4 ✅
b) 1 2 3 4
c) 1 2 4
d) 2 3 4
e) None
✅
a) 1 2 3 4 5
b) 1 2
c) 1 2 3
d) None
e) 2 3 4 5
✅
a) Exits script
b) Stops current loop
c) Skips next iteration
d) Resets loop counter
e) Ends function
✅
b) Restarts script
c) Skips current iteration
d) Restarts loop
e) Pauses execution
Explanation: continue jumps to next iteration without executing remaining loop body.
✅
a) 1
b) 0
c) -1
d) null
e) True
✅
a) 1
b) 0
c) True
d) Success
e) 2
Explanation: grep returns 0 if match is found.
a) Terminates script
✅
b) Exits shell
c) Exits the function and sets its return code
d) Repeats function
e) Restarts loop
a) 0–127 ✅
b) 1–10
c) -1 to 1
d) 0–255
e) 0–1
Explanation: Valid return codes range 0–127; codes >128 represent signals.
a) Returns 0 ✅
b) Returns -1
c) Returns null
d) Causes error
e) Infinite loop
Explanation: Default return value is the exit code of the last executed command (commonly 0).
✅
a) 0
b) 5
c) Error
d) None
e) True
✅
a) End loop
b) End script with exit code 2
c) Continue script
d) Pause for 2 seconds
e) Return 2 from function
Explanation: exit terminates script; return terminates only the current function.
Q75. Which command can you use to verify exit status inline?
Explanation: In if command; then … fi, the condition checks command’s exit status
automatically.
a) bash [Link] ✅
b) sh$ [Link]
c) exec myscript
d) run [Link]
e) execute myscript
Explanation: The script can be executed using bash [Link] or sh
[Link].
a) sh
b) bash ✅
c) csh
d) zsh
e) ksh
Explanation: Most Linux distributions use Bash (Bourne Again SHell) as the default shell.
a) //
b) --
c) # ✅
d) /* */
e) ##
Explanation: In shell scripts, # marks a comment, except when used in #! for shebang.
Q4. What does the line #!/bin/bash at the top of a shell script mean?
✅
a) Starts a bash session manually
b) Specifies bash as the script’s interpreter
c) Comments the first line
d) Imports bash libraries
e) Creates a variable
Explanation: The shebang (#!) defines which interpreter executes the script.
a) chmod +x [Link] ✅
b) bash [Link]
c) exec [Link]
d) run [Link]
e) source [Link]
Explanation: The chmod +x command adds execute permission to the script file.
a) showenv
b) env ✅
c) printenv all
d) ls -e
e) echo env
Explanation: The env command lists all environment variables.
a) #()
b) {}
c) $() ✅
d) @()
e) %()
Explanation: $(command) executes the command and substitutes its output.
Q10. How do you read user input into a variable name?
a) input name
b) scanf name
c) read name ✅
d) get name
e) prompt name
Explanation: The read command reads input from standard input.
a) x = 10
b) x==10
c) x=10 ✅
d) $x=10
e) int x=10
Explanation: No spaces are allowed around = in shell variable assignment.
Q12. How can you access the value of a variable named city?
a) city
b) ${city} ✅
c) print(city)
d) var(city)
e) $var city
Explanation: Variables are referenced with a $ prefix, e.g., $city or ${city}.
a) Causes an error
✅
b) Returns “undefined”
c) Expands to an empty string
d) Expands to NULL
e) Returns 0
Explanation: Uninitialized variables expand to an empty string in bash.
Q14. What does $0 represent in a shell script?
✅
a) Number of arguments
b) Script name
c) First argument
d) Last argument
e) Script exit code
Explanation: $0 holds the name of the currently running script.
a) $0
b) $1
c) $# ✅
d) $*
e) $@
Explanation: $# gives the total count of command-line arguments.
a) $#
b) $* ✅
c) $@
d) ${args}
e) $()
Explanation: $* expands to all arguments as one string, unlike $@.
a) unset var ✅
b) remove var
c) delete var
d) clear var
e) drop var
Explanation: The unset command deletes a shell variable or function.
a) global var=10
b) export var=10 ✅
c) share var=10
d) env var=10
e) sub var=10
Explanation: The export keyword marks a variable as part of the environment.
a) x+y
b) 15 ✅
c) $x+$y
d) 5+10
e) Error
Explanation: $((expression)) performs arithmetic expansion.
Q21. Which command displays the type of a shell variable or command?
a) check
b) typeof
c) type ✅
d) whatis
e) info
Explanation: The type command shows whether a name is a shell keyword, alias, or function.
a) Hello
b) World
c) Hello World ✅
d) Hello World
e) Error
Explanation: Double quotes preserve whitespace while expanding variables.
a) 15
b) 10+5 ✅
c) 5
d) Error
e) x+5
Explanation: Without arithmetic expansion $(( )), it performs string concatenation.
Q24. Which command prints both the variable name and its value?
a) env ✅
b) show
c) display
d) echo $var
e) printenv
Explanation: env or printenv lists variables and their values.
a) listvar
b) echo $*
c) set ✅
d) env
e) showvars
Explanation: The set command lists all variables and functions in the shell.
Q26. How do you declare a variable named count with value 5 in bash?
a) int count = 5
b) declare int count=5
c) count=5 ✅
d) $count=5
e) var count = 5
Explanation: Shell variables are created simply by assignment without spaces or type.
a) 30
b) $a+$b
c) Error ✅
d) 1020
e) Nothing
Explanation: $($a+$b) tries command substitution using an invalid command name “10+20”,
causing an error.
Q29. Correct syntax for printing the sum of arguments 2 and 3 in a script:
a) echo($2+$3)
b) echo $(($2+$3)) ✅
c) echo ($2+$3)
d) echo $2+$3
e) echo $($2+$3)
Explanation: Arithmetic expansion in bash uses $((...)).
a) 5
✅
b) 7
c) 2
d) 12
e) 1
Explanation: $# gives the number of positional parameters (here, 2 arguments).
a) Name of user
✅
b) Current shell
c) Name of script
d) Total arguments
e) Working directory
Explanation: $0 expands to the name of the current script.
✅
a) Returns argument count
b) Returns all arguments as separate strings
c) Joins all arguments into one string
d) Returns script name
e) Returns last argument
Explanation: $@ expands to all positional parameters as individual words.
Q34. What happens when no arguments are passed but $1 is used in the
script?
a) Prints 0
✅
b) Prints “null”
c) Expands to empty
d) Causes error
e) Prints “undefined”
Explanation: If no positional argument exists, $1 expands to empty string.
a) A smaller ✅
b) B smaller
c) Error
d) A smaller B smaller
e) None
Explanation: -lt tests if $a is less than $b.
a) if ($a == $b)
b) if [ $a = $b ] ✅
c) if { $a eq $b }
d) if test $a==$b
e) if $a=$b
Explanation: For string comparison, use single = within [ ].
Q37. Which operator checks for file existence?
a) -x
b) -e ✅
c) -f
d) -r
e) -s
Explanation: -e filename returns true if the file exists.
✅
a) Missing
b) Found
c) [Link]
d) None
e) Error
Explanation: -e tests for file existence.
✅
a) Missing semicolon
b) Missing then
c) Missing brackets
d) Wrong operator
e) Wrong syntax for echo
Explanation: Bash requires then keyword after condition.
Q40. Output of this script:
num=6
if (( num % 2 == 0 )); then
echo "Even"
else
echo "Odd"
fi
✅
a) Odd
b) Even
c) 6
d) num % 2
e) Error
Explanation: Double parentheses (( )) perform arithmetic evaluation.
✅
b) Medium
c) Small
d) Error
e) 4
Explanation: $x=4 fails both greater-than tests → prints “Small”.
✅
a) Syntax correctness
b) Command exit status
c) Variable value
d) File type
e) Argument count
Explanation: In bash, if evaluates the exit status of a command (0 = true).
a) Hi
b) Yes
✅
c) Hi
Yes
d) Error
e) None
Explanation: echo always succeeds (exit code 0), so both lines print.
✅
a) Error
b) ok
c) 2
d) Division not supported
e) None
Explanation: Arithmetic expression ((a / b == 2)) evaluates true.
a) 10 ✅
b) 25
c) 5
d) $x+5
e) Error
Explanation: Condition true → prints arithmetic result 5+5=10.
a) for i in range(1,5)
b) for i=1 to 5
c) for i in 1 2 3 4 5; do echo $i; done ✅
d) for(i=1;i<=5;i++) echo $i
e) foreach i (1..5)
Explanation: Bash uses for var in list; do ...; done.
a) 1
2
3
✅
b) 123
c) 1 2 3
d) “i” three times
e) Error
Explanation: -n prevents newline, so prints 1 2 3.
✅
a) 1 2 3
b) 1 4 9
c) 2 4 6
d) 3 6 9
e) Error
Explanation: C-style loop syntax works in bash arithmetic context.
a) for
✅
b) until
c) while
d) foreach
e) repeat
Explanation: while runs while condition evaluates true (exit code 0).
✅
a) 123
b) 1 2 3
c) Infinite loop
d) 4 5 6
e) Error
Explanation: Increments till 3 → prints 1 2 3.
Q57. Which loop executes as long as condition is false?
✅
a) for
b) until
c) while
d) loop
e) repeat
Explanation: until continues until condition becomes true.
✅
a) 1 2
b) 1 2 3
c) None
d) Infinite loop
e) Error
Explanation: until stops when $n > 3`.
✅
a) 1 2 3 4 5
b) 1 2 4 5
c) 3 4 5
d) 1 2
e) None
Explanation: continue skips iteration 3.
✅
a) Lists all files in directory
b) Lists only .txt files
c) Syntax error
d) Lists all hidden files
e) Prints nothing
Explanation: Command substitution $(ls *.txt) iterates over .txt files.
Q62. Output:
sum=0
for i in {1..3}
do
((sum+=i))
done
echo $sum
✅
a) 3
b) 6
c) 5
d) 1 2 3
e) Error
Explanation: Sum = 1 + 2 + 3 = 6.
✅
a) It repeats last line
b) It exits loop
c) Returns 1 and continues
d) Throws error
e) Resets variable
Explanation: read returns non-zero on EOF → loop ends.
Q65. What is printed?
for i in {1..3}
do
for j in {1..2}
do
echo "$i,$j"
done
done
✅
a) 1,1 1,2 2,1 2,2 3,1 3,2
b) 1,1 2,1 3,1 1,2 2,2 3,2
c) Only 3,2
d) Syntax error
e) Infinite
Explanation: Nested for loop produces 3 × 2 = 6 pairs.
Q66. What will be the exit status after executing ls /etc > /dev/null
successfully?
a) 0 ✅
b) 1
c) 127
d) 255
e) Depends
Explanation: Exit code 0 → command succeeded.
Q67. Which variable stores the exit code of the last executed command?
a) $#
b) $@
c) $? ✅
d) $!
e) $:
Explanation: $? holds last command’s exit status.
Q68. What will $? contain after this?
grep "admin" /etc/passwd
✅
a) 0
b) 1
c) 2
d) 127
e) 255
Explanation: grep returns 1 if no match found.
✅
a) 0
b) 1
c) 2
d) 255
e) None
Explanation: false always returns exit code 1.
a) 0✅
b) 1
c) true
d) false
e) None
Explanation: true always returns exit code 0.
Q71. Which command explicitly exits a shell script with code 5?
a) stop 5
b) return 5
c) exit 5 ✅
d) break 5
e) end 5
Explanation: exit <code> terminates script with given status.
✅
a) No difference
b) exit ends script, return ends function
c) exit ends loop, return repeats
d) exit resets variables
e) return is invalid in bash
Explanation: return only valid inside functions; exit ends entire script.
✅
a) 0
b) 3
c) 1
d) 255
e) Error
Explanation: $? captures return status 3 from function.
✅
b) 1
c) “done” then 1
d) “done” then 0
e) Error
Explanation: ls fails → exit code 1; function continues but $? = 1.
Q75. Which of the following commands will execute a script and pass its
exit status to the shell?
a) . [Link] ✅
b) bash < [Link]
c) source [Link] ✅
d) run [Link]
e) both a and c ✅
Explanation: Both . and source run scripts in current shell, preserving exit status.
✅
Q4. Which command gives execution permission to a shell script?
a) chmod +x [Link]
b) execute [Link]
c) bash +x [Link]
d) permit [Link]
e) run +x [Link]
Explanation: chmod +x makes a script executable.
a) Nothing
b) Shell scripting
c) Shell scripting is fun ✅
d) Error
e) fun
Explanation: echo simply outputs the given text.
✅
b) Number of arguments
c) Name of the shell or script
d) Exit status
e) PID of script
Explanation: $0 stores the name of the script.
✅
Q8. Which command prints the current working directory?
a) pwd
b) dir
c) cd
d) ls
e) path
Explanation: pwd (print working directory) displays the current directory.
Q9. What is the correct way to run a script in the current directory?
a) bash [Link]
b) ./[Link] ✅
c) /[Link]
d) run [Link]
e) source ./[Link]
Explanation: ./[Link] executes it using relative path.
✅
Q10. Which of the following commands lists all environment variables?
a) set
b) ls -e
c) show env
d) printenv
e) export
Explanation: set or printenv can display environment variables.
Q11. What does this command output?
echo $(date)
✅
a) Nothing
b) Prints current date
c) Error
d) Prints $(date)
e) Prints only year
Explanation: Command substitution executes date and echoes its output.
✅
a) Script won’t execute
b) Script executes using default shell
c) System crash
d) Syntax error
e) Infinite loop
Explanation: Without shebang, system uses the default shell to interpret.
✅
b) Shell scripts must be compiled before running
c) Shell scripts are interpreted
d) Shell scripts are binary files
e) Shell scripts cannot take arguments
Explanation: Bash scripts are interpreted line by line.
Q15. What is the output of:
✅
a) Prints literal text “Hello $USER”
b) Prints “Hello root” or current user
c) Error
d) Prints username only
e) None
Explanation: $USER expands to the logged-in username.
x=10
echo $x
a) x=10
b) $x
c) 10 ✅
d) Error
e) Null
Explanation: $x expands to the variable’s value.
a=5
b=10
echo $(($a+$b))
a) $a+$b
b) 5+10
c) 15 ✅
d) a+b
e) Error
Explanation: $((...)) performs arithmetic evaluation.
✅
Q19. Which command lists all environment variables?
a) env
b) echo $env
c) printvars
d) set -a
e) ls -e
Explanation: env displays exported environment variables.
x=10
y=$x+2
echo $y
a) 12
b) 10+2 ✅
c) 2
d) Error
e) Null
Explanation: Without $(( )), bash treats + as a string, not arithmetic.
x=2
y=3
echo $(($2+$3))
a) 5
b) 2+3
c) Error ✅
d) 0
e) $2+$3
Explanation: $2 and $3 refer to positional arguments, not variables x and y. Undefined →
empty → arithmetic error.
num1=8
num2=4
result=$((num1/num2))
echo $result
a) 2 ✅
b) 2.0
c) 0
d) Error
e) num1/num2
Explanation: Bash integer division returns integer value 2.
name="Harsh"
echo "Hello $name"
a) $name
b) Hello Harsh ✅
c) Hello $name
d) Harsh
e) None
Explanation: $name expands to its stored value.
x=3
y=2
z=$((x**y))
echo $z
a) 9 ✅
b) 6
c) 8
d) Error
e) x**y
Explanation: ** is the exponentiation operator in bash arithmetic.
Q27. Which command prints all defined shell variables (local + environment)?
a) env
b) set ✅
c) export
d) declare -x
e) vars
Explanation: set lists both shell and environment variables.
var=123
var=456
echo $var
a) 123
b) 456 ✅
c) 123456
d) Error
e) Null
Explanation: Latest assignment overwrites the previous value.
✅
Q29. Which statement is true about variable scope?
a) All variables are global by default
b) All variables are local by default
c) Only constants are global
d) Variables need explicit global keyword
e) Variables cannot be made global
Explanation: In bash, variables are global unless defined with local in a function.
name="IFSCA"
echo '${name}'
a) IFSCA
b) ${name} ✅
c) $name
d) Error
e) None
Explanation: Single quotes prevent variable expansion; $ treated literally.
Q31. What does $1 represent in a shell script?
a) Number of arguments passed
✅
b) Name of the script
c) First argument
d) Last argument
e) Exit status
Explanation: $1 stores the first argument passed to the script.
✅
a) Sum of all arguments
b) Count of arguments
c) Name of the shell
d) Exit code
e) Total bytes
Explanation: $# gives the number of arguments passed.
✅
a) Last argument
b) Script name
c) Path of current directory
d) Total arguments
e) None
Explanation: $0 stores the script name or path used to execute it.
echo $(($1+$2))
a) 5 ✅
b) $1+$2
c) 23
d) 2+3
e) Error
Explanation: $1 = 2, $2 = 3 → arithmetic evaluation gives 5.
Q35. What is the output of:
echo $*
echo "$@"
✅
a) Resets variables
b) Removes first argument
c) Clears all arguments
d) Copies arguments
e) Reverses arguments
Explanation: shift discards $1 and shifts others down.
echo $1
shift
echo $1
echo "$@"
shift 2
echo "$@"
✅
Q43. How do you handle optional arguments with defaults in bash?
a) Use if condition on $#
b) Declare argument type
c) Use read
d) Use input()
e) You can’t
Explanation: You can check if $# < expected count and assign default.
echo "$@"
set "A" "B" "C"
echo "$#"
a) 0
b) 3 ✅
c) 1
d) Error
e) $@
Explanation: set redefines positional parameters; $# = 3.
✅ Covered concepts: $0, $1, $#, $*, $@, shift, ${!#}, argument defaults.
Q46. What is the correct syntax of an if statement in bash?
a) if (condition) then ... fi
b) if [ condition ] then ... fi ✅
c) if { condition } fi
d) if (condition) fi
e) if condition; end
Explanation: The correct structure is if [ condition ]; then ... fi.
a=5
if [ $a -eq 5 ]
then
echo "Equal"
else
echo "Not Equal"
fi
a) Error
b) Equal ✅
c) Not Equal
d) $a=5
e) None
Explanation: -eq compares integers for equality.
x="IFSCA"
if [ "$x" == "IFSCA" ]; then echo "Match"; else echo "No"; fi
a) IFSCA
b) Match ✅
c) No
d) Error
e) $x
Explanation: Strings match, condition true.
if [ -d /home/user ]
a) Regular file
✅
b) Empty file
c) Directory
d) Executable file
e) Hidden file
Explanation: -d tests if the path is a directory.
x=4
if [ $x -lt 10 -a $x -gt 2 ]; then echo "True"; fi
a) False
b) True ✅
c) Error
d) None
e) 0
Explanation: -a = logical AND; both conditions true.
str="IFSCA"
if [ -n "$str" ]; then echo "Not Empty"; else echo "Empty"; fi
a) Empty
b) Not Empty ✅
c) IFSCA
d) Error
e) None
Explanation: -n checks if string length > 0.
num=8
if (( num % 2 == 0 )); then echo "Even"; fi
a) Error
b) 8
c) Even ✅
d) False
e) None
Explanation: Double parentheses (( )) allow arithmetic comparisons.
✅
a) Nothing
b) Syntax error
c) Condition skips
d) Prints 0
e) Default fi assumed
Explanation: Every if must end with fi.
x=5
if [ $x -eq 5 ]; then
echo "A"
elif [ $x -eq 10 ]; then
echo "B"
else
echo "C"
fi
a) B
b) C
c) A ✅
d) 5
e) None
Explanation: First condition true → prints “A”.
✅ Covered concepts: syntax, [ ] vs [[ ]], numeric and string operators, logical AND/OR,
file tests, nesting, and edge cases.
a) 0 ✅
b) 1
c) 10
d) 20
e) Syntax error
Explanation: The expression returns 1 if true and 0 if false. Since 10 != 20, result is 0.
✅
a) 9
b) 5
c) 7
d) 10
e) Error
Explanation: $2=7, $3=2 → 7+2=9, but here $($2 + $3) is evaluated as $7 + $3 which
are empty variables, so error → fixed form should be $(( $2 + $3 )).
Explanation: The export command marks a variable for export to child processes.
Q29. Which of the following is the correct syntax to read a value from user
input in a shell script?
a) input = read
b) read input ✅
c) read = input
d) get input
e) scanf input
✅
a) Error
b) True
c) False
d) 5 10
e) None
Q31. Which of the following statements correctly checks if a file exists and
is readable?
a) [ -r $file ] ✅
b) [ -x $file ]
c) [ -f $file ]
d) [ $file -eq 0 ]
e) [ read $file ]
a) 0
✅
b) Infinity
c) Division by zero error
d) Nothing
e) 5
a) 1
b) -1
✅
c) 255
d) 0
e) None
a) 3 4 5 6 7✅
b) 1 2 3 4 5
c) 7 6 5 4 3
d) 3..7
e) Error
a) while (true)
b) while true; do ... done ✅
c) loop forever
d) for(;;)
e) repeat do done
✅
a) Shifts positional parameters to the right
b) Shifts positional parameters to the left
c) Deletes $1 only
d) Resets all arguments
e) None
Explanation: Each call to shift discards $1 and moves $2 → $1, $3 → $2, etc.
a) 4 ✅
b) 10
c) 3
d) 7
e) None
a) #
b) $() ✅
c) @()
d) []
e) {}
a) 1 ✅
b) 2
c) 0
d) 8
e) Error
a) [ -e $str ]
b) [ -n $str ]
c) [ -z $str ] ✅
d) [ $str == 0 ]
e) None
Q41. What will the output be?
#!/bin/bash
a=10
b=5
if [ $a -gt 5 ] && [ $b -lt 10 ]; then
echo "Condition met"
else
echo "Condition failed"
fi
✅
a) Condition failed
b) Condition met
c) Error
d) None
e) 10 5
✅
a) Script runs normally
b) Syntax error
c) It ignores condition
d) fi is optional
e) None
✅
a) Number of background processes
b) Number of arguments passed
c) Process ID of shell
d) The exit code
e) None
a) No difference
b) $* preserves spaces; $@ doesn’t
c) $@ treats each argument separately ✅
d) $* gives total count
e) Both expand only first argument
a) 1 2 3 ✅
b) 123
c) 1\n2\n3
d) Error
e) None
a) 1 2 3 4 5
b) 1 3 5 ✅
c) 2 4
d) Error
e) None
a) Yes ✅
b) No
c) 15
d) Error
e) None
a) 0 ✅
b) 1
c) 255
d) -1
e) None
✅
a) 15
b) 255
c) 0
d) 10
e) Error
Explanation: Bash return can only return 0–255. Here 15 is valid, but $? returns it as an exit
code.
a) var = cmd
b) var=$(cmd) ✅
c) var=cmd
d) $var=cmd
e) var => cmd
a) 2 4 6 ✅
b) 2 3 4 5 6
c) 2 4
d) 6 4 2
e) None
a) 0 1 2
b) 1 2 3 ✅
c) seq 3
d) Error
e) None
✅
a) Prints the full line
b) Prints each word on a new line
c) Error
d) Prints only “IFSCA”
e) None
✅
a) Skips next iteration
b) Stops loop execution
c) Jumps to top
d) Restarts loop
e) None
✅
a) Exits loop
b) Skips current iteration
c) Ends script
d) Restarts program
e) None
a) 1 2 3 ✅
b) 0 1 2
c) 1 2 3 4
d) Infinite loop
e) None
Q62. What happens here?
#!/bin/bash
n=3
until [ $n -eq 0 ]
do
echo $n
((n--))
done
a) 3 2 1 ✅
b) Infinite loop
c) 0 1 2
d) None
e) Error
a) ==
b) -eq ✅
c) =
d) is
e) equals
a) [ -d /path/to/dir ] ✅
b) [ -f /path/to/dir ]
c) [ dir /path ]
d) [ -e file ]
e) [ -x path ]
✅
a) 10
b) 20
c) 30
d) 0
e) Error
✅
a) The PID
b) The exit status of the last command
c) Total number of arguments
d) Shell version
e) None
✅
a) Ignores errors
b) Exits immediately if a command fails
c) Enables echoing
d) Prints environment
e) None
a) 5
✅
b) 10
c) 15
d) 20
e) Error
Q69. What will the output of this script be?
#!/bin/bash
num=7
if [ $((num % 2)) -eq 0 ]; then
echo "Even"
else
echo "Odd"
fi
✅
a) Even
b) Odd
c) Error
d) None
e) 7
✅
a) 6
b) 9
c) 8
d) Error
e) None
a) 2+3
b) 5 ✅
c) bc
d) Error
e) None
Command: ./[Link] 4 5 6
a) 11
b) Error ✅
c) 9
d) 56
e) None
a) echo $# ✅
b) echo $@
c) echo $*
d) echo $$
e) echo $?
✅
a) Arguments: A B C D | Count: 4
b) Arguments: A B C D | Count: 3
c) Arguments: A B | Count: 2
d) Error
e) None
✅
a) Prints names of files
b) Prints total number of files
c) Prints all file sizes
d) Syntax error
e) None
a) Total: 7 ✅
b) Total: expr 3 + 4
c) Error
d) Total:
e) None
✅
a) Prints lines containing bash
b) Counts lines containing “bash”
c) Deletes lines containing “bash”
d) Replaces “bash”
e) None
✅
a) Prints all fields
b) Prints username and UID of first two users
c) Prints only usernames
d) Error
e) None
Q81. What does this script output?
#!/bin/bash
x=$(grep root /etc/passwd | wc -l)
echo $x
✅
a) Always prints “File exists”
b) Prints “File exists” if [Link] present
c) Prints “Not found” always
d) Error
e) None
a) /home/user
b) [Link] ✅
c) /home
d) None
e) user
✅
a) Current date
b) Current year
c) 2025 only
d) Error
e) None
Command: ./[Link] 3 5
✅
a) 8
b) 15
c) 35
d) Error
e) None
✅
a) Last argument
b) Script name
c) First argument
d) Exit code
e) None
a) Hello✅
b) echo Hello
c) Error
d) None
e) Hello Hello
✅
a) 1 2 3
b) 1 3
c) 2 3
d) 1 2
e) None
a) A B C ✅
b) ABC
c) Array
d) None
e) Error
✅
a) Shell name
b) Current logged-in user
c) Root
d) Hostname
e) None
Q97. Which of the following prints last 5 lines of a file?
a) head -n 5 file
b) tail -n 5 file ✅
c) grep 5 file
d) awk -n5 file
e) None
✅
a) IFSCAExam
b) IFSCA_Exam
c) _Exam
d) Error
e) None
a) cat /etc/passwd | wc -l ✅
b) ls /home | wc
c) who
d) id
e) grep users