0% found this document useful (0 votes)
3 views91 pages

Shell Scripting

Uploaded by

Srijan Shukla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views91 pages

Shell Scripting

Uploaded by

Srijan Shukla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1.

Which of the following commands correctly assigns a value to a shell


variable?

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.

🧩 Shell Scripting Basics & Variables (Q1–Q25)


Q1. Which of the following lines correctly defines the shell interpreter for a
Bash script?

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.

Q2. Which command makes a shell script executable?

a) run [Link]​
b) chmod +x [Link] ✅​
c) exec [Link]​
d) bash -x [Link]​
e) set +x [Link]

Explanation: chmod +x gives execution permission to the script file.


Q3. Which command is used to execute a shell script in the current shell
context?

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.

Q4. How do you display the current shell being used?

a) echo $0 ✅​
b) cat /etc/shell​
c) ls -s​
d) show shell​
e) whoami

Explanation: $0 contains the name of the shell or script being executed.

Q5. Which of the following is not a valid shell in UNIX?

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.

Q6. What is the output of:


x=10
y=20
echo $x+$y
a) 30​
b) 1020​

✅​
c) $x+$y​
d) 10+20
e) Error

Explanation: Variables are expanded but not evaluated arithmetically without expr or $(( )).

Q7. What is the output of:


num=5
echo "$num*2"


a) 10​
b) 5*2 ​
c) *2​
d) 7​
e) Error

Explanation: The * is not interpreted as multiplication; use $((num*2)) for arithmetic.

Q8. Which command allows arithmetic operations in Bash?

a) add​
b) calc​
c) $(( )) ✅​
d) expr[]​
e) math()

Explanation: Arithmetic expansion in Bash is done using $((expression)).

Q9. How can you read user input in Bash?

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

Explanation: Syntax: read variable_name assigns input to that variable.

Q11. Which of the following commands shows the exit status of the last
executed command?

a) $!​
b) $@​
c) $? ✅​
d) $#​
e) $$

Explanation: $? holds the exit status of the last command executed.

Q12. What is the output of:


x=7
((x++))
echo $x

a) 6​

✅​
b) 7​
c) 8
d) ++7​
e) Syntax Error

Explanation: Post-increment increases x by 1 after evaluation; final value is 8.

Q13. Which of the following variables stores the process ID of the current
shell?

a) $@​
b) $#​
c) $$ ✅​
d) $?​
e) $PID

Explanation: $$ expands to the PID of the current shell process.

Q14. What does set -x do in a shell script?


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.

Q15. Which of the following commands unsets a variable?

a) clear var​
b) remove var​
c) unset var ✅​
d) delete var​
e) erase var

Explanation: unset deletes a variable from the shell environment.


Q16. What is the difference between $var and ${var} in shell?

a) No difference ​ ✅
b) ${var} gives value, $var gives address​
c) $var is slower​
d) ${var} creates array​
e) $var is deprecated

Explanation: Both reference variable values; braces help in concatenation clarity.

Q17. What is the output of:


var="World"
echo "Hello $var!"

a) Hello World! ✅​
b) Hello var!​
c) Hello$var!​
d) World!​
e) Error

Explanation: Variable substitution replaces $var with its value.

Q18. Which command lists all shell variables and functions?

a) echo $*​
b) set ✅​
c) showvars​
d) env​
e) vars

Explanation: set without arguments lists all variables and functions.

Q19. Which statement correctly concatenates two variables a and b?

a) c = $a$b​
b) c=$a$b ✅​
c) c=a+b​
d) c=${a+b}​
e) concat a b

Explanation: Concatenation in bash is done by directly adjoining variables.

Q20. What is the output of:


a=5
b=10
echo $((a>b))

a) 5​
b) 10​


c) true​
d) 0 ​
e) 1

Explanation: Returns 1 if true, 0 if false; here a>b is false.

Q21. What happens if you execute a script without #!/bin/bash?

✅​
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

Explanation: If no shebang is present, system uses default shell (e.g., /bin/sh).

Q22. What is the default value of an uninitialized variable?

a) null​
b) 0​
c) Empty string ✅​
d) undefined​
e) NaN
Explanation: Uninitialized variables expand to an empty string by default.

Q23. Which option of echo prevents automatic newline at the end?

a) -l​
b) -n ✅​
c) -e​
d) -E​
e) -s

Explanation: echo -n omits the trailing newline.

Q24. What is the output of:


echo $((2+3*2))

a) 10 ✅ ​
b) 8​
c) 7​
d) 12​
e) Error

Explanation: Follows normal arithmetic precedence — multiplication before addition.

Q25. What is the purpose of export command in shell?

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

Explanation: export marks variables to be inherited by subshells or child processes.

🧩 Shell Script Arguments & If Statements (Q26–Q50)


Q26. In a shell script, $0 refers to:

a) The first argument​


b) The number of arguments​
c) The script name ​
d) The last argument​
e) Exit status

Explanation: $0 holds the name of the script being executed.

Q27. $1, $2, $3, … represent:

✅​
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.

Q28. Which variable gives the total number of command-line arguments?

a) $@​
b) $*​
c) $# ✅​
d) $?​
e) $0

Explanation: $# expands to the count of command-line arguments.

Q29. Which of the following prints all command-line arguments as a single


string?

a) $@​
b) $* ✅​
c) $#​
d) $?​
e) $0

Explanation: $* expands all positional parameters as one string.

Q30. Which of the following prints all arguments as separate quoted


strings?

a) $@ ✅​
b) $*​
c) $#​
d) $?​
e) $0

Explanation: $@ treats each argument as an individual word.

Q31. What is the output of:


echo "First: $1 Second: $2"

if the script is run as ./[Link] hello world?​



a) First: hello Second: world ​
b) First: world Second: hello​
c) First: $1 Second: $2​
d) First: [Link] Second: hello​
e) Error

Explanation: $1 = first argument, $2 = second argument.

Q32. If no arguments are passed, what is $# equal to?


a) 1​
b) 0 ​
c) null​
d) undefined​
e) -1
Explanation: $# shows count of arguments; if none passed, it’s 0.

Q33. What is the output of:


echo "$*"

if the script is run as: ./[Link] one two three​


a) one two three✅ ​
b) "one" "two" "three"​
c) one​
d) three​
e) Error

Explanation: $* joins all arguments separated by the first character of IFS (default space).

Q34. How to check if at least one argument was passed to a script?

a) if $# -gt 1​
b) if [ $# -ge 1 ] ✅​
c) if $# == 1​
d) if [ $# = 0 ]​
e) if test $#

Explanation: [ $# -ge 1 ] checks if the count of arguments is ≥ 1.

Q35. What will $@ expand to inside double quotes?

✅​
a) All arguments as one word​
b) Each argument as separate word
c) No expansion​
d) Only first argument​
e) Error

Explanation: "$@" expands each argument separately, preserving spacing.


Q36. What does the if statement test in shell scripts?

✅​
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

Explanation: test (or [ ]) checks file existence, strings, numbers, etc.

Q38. Which syntax is correct for if in Bash?

a) if (condition)​
b) if { condition }​
c) if [ condition ]; then ... fi ✅​
d) if < condition > fi​
e) if { condition; done }

Explanation: The correct structure is if [ condition ]; then ... fi.

Q39. What is the output of:


a=10
b=20
if [ $a -lt $b ]; then
echo "Smaller"
fi
✅​
a) Larger​
b) Smaller
c) Equal​
d) Error​
e) None

Explanation: $a -lt $b evaluates true, printing “Smaller”.

Q40. Which operator checks equality of two strings?

a) -eq​
b) -lt​
c) == ✅​
d) =​
e) -eqs

Explanation: For strings, == (inside [[ ]]) checks equality.

Q41. What is the output of:


str="hello"
if [ "$str" == "hello" ]; then
echo "Match"
else
echo "No"
fi


a) hello​
b) Match ​
c) No​
d) Error​
e) hello hello

Explanation: Condition true → prints “Match”.


Q42. Which syntax checks if file [Link] exists?

a) [ -e [Link] ] ✅​
b) [ -x [Link] ]​
c) [ -f [Link] ]​
d) [ [Link] ]​
e) [ -d [Link] ]

Explanation: -e tests file existence (of any type).

Q43. How to check if a variable x is empty?

a) [ $x == null ]​
b) [ -n $x ]​
c) [ -z $x ] ✅​
d) [ empty $x ]​
e) [ $x -eq 0 ]

Explanation: -z returns true if the string length is zero.

Q44. What is the output of:


num=15
if (( num % 5 == 0 )); then
echo "Divisible"
else
echo "Not"
fi

a) 3​

✅​
b) 0​
c) Divisible
d) Not​
e) Error

Explanation: Arithmetic evaluation uses (( )), result is true → “Divisible”.


Q45. Which construct checks if file [Link] is a regular file?

a) [ -d [Link] ]​
b) [ -r [Link] ]​
c) [ -f [Link] ] ✅​
d) [ -s [Link] ]​
e) [ -a [Link] ]

Explanation: -f returns true if a regular file exists.

Q46. Which syntax correctly performs logical AND in if?

a) if [ cond1 & cond2 ]​


b) if [ cond1 -and cond2 ]​
c) if [ cond1 ] && [ cond2 ] ✅​
d) if [ cond1 && cond2 ]​
e) if (cond1, cond2)

Explanation: Bash uses && between two conditions for AND logic.

Q47. Which syntax performs logical OR in if?

a) if [ cond1 || cond2 ]​
b) if [ cond1 -or cond2 ]​
c) if [ cond1 ] || [ cond2 ] ✅​
d) if cond1 or cond2​
e) if {cond1 || cond2}

Explanation: Use || for OR between test statements.

Q48. What is the output of:


a=5
if [ $a -ge 10 ]; then
echo "High"
else
echo "Low"
fi


a) High​
b) Low ​
c) Error​
d) Nothing​
e) 10

Explanation: 5 -ge 10 is false, so “Low” is printed.

Q49. Which command tests if file [Link] has read permission?

a) [ -r [Link] ] ✅​
b) [ -p [Link] ]​
c) [ -s [Link] ]​
d) [ -x [Link] ]​
e) [ -a [Link] ]

Explanation: -r checks if the file is readable.

Q50. What does elif mean in shell scripting?

a) Else-if condition ✅​
b) Else loop​
c) End of file​
d) Else final​
e) End of if

Explanation: elif provides an additional condition when the previous if fails.

🧩 Loops and Return/Exit Status (Q51–Q75)


Q51. Which is the correct syntax of a for loop in Bash?
a) for (i=0;i<10;i++)​
b) for i in 1..10​
c) for i in {1..10}; do ... done ✅​
d) for(i=1;i<=10;i++)do​
e) for each i in 1 to 10

Explanation: Bash uses for var in list; do … done syntax. {1..10} generates a
sequence.

Q52. What is the output of:


for i in 1 2 3
do
echo $i
done

a) 123​
b) 1 2 3​
c)

1
2
3
``` ✅
d) [1,2,3]
e) Error

**Explanation:** Each element is printed on a new line by default.

---

### **Q53.** What is the output of:


```bash
for i in {1..3}
do
echo -n "$i "
done

a) 123​
b) 1 2 3 ​
c) {1..3}​
d) Error​
e) 1\n2\n3

Explanation: {1..3} expands to 1 2 3; -n prevents newlines.

Q54. How do you create a C-style for loop in Bash?

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)

Explanation: Bash supports for ((init; condition; increment)) syntax.

Q55. What will this code print?


for ((i=1;i<=3;i++))
do
echo "Count $i"
done

a) Count 1 Count 2 Count 3 ✅​


b) 1 2 3​
c) Count 1​
d) Error​
e) i=1 i=2 i=3

Explanation: C-style loop iterates 3 times, printing the counter.

Q56. Which loop is used when the number of iterations is not known?

a) for​
b) until​
c) while ✅ ​
d) repeat​
e) foreach

Explanation: while is preferred for condition-based indefinite iteration.

Q57. What does this script do?


count=1
while [ $count -le 3 ]
do
echo "Hello $count"
((count++))
done

✅​
a) Infinite loop​
b) Prints Hello 1 Hello 2 Hello 3
c) Syntax error​
d) Prints 1, 2, 3 only​
e) Prints 4 times

Explanation: The loop runs 3 times until $count becomes 4.

Q58. Which loop runs until a condition becomes true?

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).

Q59. What is the output of:


x=1
until [ $x -gt 3 ]
do
echo $x
((x++))
done


a) 1 2​
b) 1 2 3 ​
c) Infinite loop​
d) 1 2 3 4​
e) None

Explanation: Executes until $x -gt 3 becomes true, printing 1–3.

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.

Q61. What is the output of:


for i in 1 2 3 4
do
[ $i -eq 3 ] && break
echo $i
done


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

Explanation: continue skips the iteration when i==2.

Q63. What is the output of:


for i in {1..5}
do
((i==3)) && exit
echo $i
done


a) 1 2 3 4 5​
b) 1 2 ​
c) 1 2 3​
d) None​
e) 2 3 4 5

Explanation: exit terminates the entire script when i==3.

Q64. What is the purpose of break in shell scripting?

✅​
a) Exits script​
b) Stops current loop
c) Skips next iteration​
d) Resets loop counter​
e) Ends function

Explanation: break exits the innermost loop immediately.

Q65. What is the purpose of continue in shell scripting?

a) Ends the script​

✅​
b) Restarts script​
c) Skips current iteration
d) Restarts loop​
e) Pauses execution

Explanation: continue jumps to next iteration without executing remaining loop body.

Q66. What does $? return after successful execution of a command?


a) 1​
b) 0 ​
c) -1​
d) null​
e) True

Explanation: Exit status 0 means success; nonzero means failure.

Q67. What is the output of:


grep "word" [Link]
echo $?

if the word exists in [Link]?​


a) 1​
b) 0 ​
c) True​
d) Success​
e) 2
Explanation: grep returns 0 if match is found.

Q68. What does return do inside a shell function?

a) Terminates script​

✅​
b) Exits shell​
c) Exits the function and sets its return code
d) Repeats function​
e) Restarts loop

Explanation: return exits a function, optionally returning a numeric exit status.

Q69. What is the range of values for return in a shell function?

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.

Q70. What happens if a function ends without an explicit return?

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).

Q71. What is the output of:


function test() {
return 5
}
test
echo $?


a) 0​
b) 5 ​
c) Error​
d) None​
e) True

Explanation: $? captures the return value (5) from the function.

Q72. What will exit 2 do inside a script?

✅​
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 ends the entire script with specified status.

Q73. What is the difference between exit and return?

a) Both are same​


b) exit exits function, return exits script​
c) exit ends script, return exits function ✅​
d) return restarts script​
e) exit restarts shell

Explanation: exit terminates script; return terminates only the current function.

Q74. What is the exit status of:


false
echo $?

a) 0​
b) 1 ​
c) 255​
d) -1​
e) True

Explanation: false returns exit code 1 (failure).

Q75. Which command can you use to verify exit status inline?

a) if command; then ... fi ✅​


b) test command​
c) check command​
d) echo $?​
e) case $? in

Explanation: In if command; then … fi, the condition checks command’s exit status
automatically.

🧠 Shell Scripting – Set 1 (Q1–Q25)


Q1. Which command is used to execute a shell script named
[Link]?

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].

Q2. What is the default shell in most modern Linux systems?

a) sh​
b) bash ✅​
c) csh​
d) zsh​
e) ksh​
Explanation: Most Linux distributions use Bash (Bourne Again SHell) as the default shell.

Q3. Which symbol is used to denote comments in a shell script?

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.

Q5. How do you make a shell script executable?

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.

Q6. How do you display the current shell being used?


a) echo $SHELL ✅​
b) print SHELL​
c) show shell​
d) cat /etc/shells​
e) env shell​
Explanation: $SHELL contains the path to the current shell.

Q7. Which of the following correctly prints “Hello World” in bash?

a) cout << "Hello World"​


b) print("Hello World")​
c) echo "Hello World" ✅​
d) printf("Hello World")​
e) display "Hello World"​
Explanation: echo is used to print output to the terminal.

Q8. Which command displays all environment variables?

a) showenv​
b) env ✅​
c) printenv all​
d) ls -e​
e) echo env​
Explanation: The env command lists all environment variables.

Q9. Which symbol is used for command substitution?

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.

Q11. Which of the following correctly assigns a value to a variable?

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}.

Q13. What happens if a variable is used without being initialized?

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.

Q15. Which variable holds the number of arguments passed to a script?

a) $0​
b) $1​
c) $# ✅​
d) $*​
e) $@​
Explanation: $# gives the total count of command-line arguments.

Q16. Which of the following prints all arguments as a single string?

a) $#​
b) $* ✅​
c) $@​
d) ${args}​
e) $()​
Explanation: $* expands to all arguments as one string, unlike $@.

Q17. What is the difference between $* and $@?

a) $* prints variables, $@ prints arguments​


b) $* joins arguments, $@ treats them separately ✅​
c) $* is numeric, $@ is string​
d) Both are identical always​
e) $* includes $0​
Explanation: $* merges all args into one string, $@ keeps them separate.

Q18. Which command removes a variable from the environment?

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.

Q19. How do you export a variable so that it is available to subprocesses?

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.

Q20. What will be the output of this script?


x=5
y=10
echo $((x+y))

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.

Q22. What will echo "$x" print if x="Hello World"?

a) Hello​
b) World​
c) Hello World ✅​
d) Hello World​
e) Error​
Explanation: Double quotes preserve whitespace while expanding variables.

Q23. What is the output of this code?


x=10
y=$x+5
echo $y

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.

Q25. Which command displays all currently defined shell variables?

a) listvar​
b) echo $*​
c) set ✅​
d) env​
e) showvars​
Explanation: The set command lists all variables and functions in the shell.

🧩 Shell Scripting – Set 2 (Q26–Q50)


(Moderate → Complex mix)

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.

Q27. What is the output of this code?


x=3
y=4
echo $((x*y + 2))

a) 12​
b) 14 ​
c) 20​
d) xy+2​
e) Error​
Explanation: $((...)) performs arithmetic expansion → 34+2 = 14.

Q28. What is the output of:


a=10
b=20
echo $($a+$b)

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 $((...)).

Q30. If a script [Link] is executed as ./[Link] 5 7, what will $# return?

a) 5​


b) 7​
c) 2 ​
d) 12​
e) 1​
Explanation: $# gives the number of positional parameters (here, 2 arguments).

Q31. What is printed by the following?


echo "Script name: $0"

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.

Q32. Output of:


echo "First: $1 Second: $2"

when script is run as ./[Link] A B​


a) First: A Second: B ✅​
b) First: B Second: A​
c) First: $1 Second: $2​
d) First: [Link] Second: A​
e) Error​
Explanation: $1 and $2 hold first and second command-line arguments.

Q33. What is $@ used for?


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.

Q35. What is the output?


a=5
b=7
if [ $a -lt $b ]; then
echo "A smaller"
else
echo "B smaller"
fi

a) A smaller ✅​
b) B smaller​
c) Error​
d) A smaller B smaller​
e) None​
Explanation: -lt tests if $a is less than $b.

Q36. Choose valid string comparison in bash:

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.

Q38. What does this script output if file [Link] exists?


if [ -e [Link] ]; then
echo "Found"
else
echo "Missing"
fi


a) Missing​
b) Found ​
c) [Link]​
d) None​
e) Error​
Explanation: -e tests for file existence.

Q39. Identify the error:


if [ $a -eq 5 ]
echo "Equal"
fi


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.

Q41. How can you test multiple conditions in bash?

a) if [ cond1 ] and [ cond2 ]​


b) if [ cond1 -a cond2 ] ✅​
c) if cond1 && cond2 ✅


d) Both b and c ​
e) None​
Explanation: -a (within [ ]) or && (outside) combine conditions logically.

Q42. What will be printed?


x=4
if [ $x -gt 10 ]; then
echo "Large"
elif [ $x -gt 5 ]; then
echo "Medium"
else
echo "Small"
fi
a) Large​


b) Medium​
c) Small ​
d) Error​
e) 4​
Explanation: $x=4 fails both greater-than tests → prints “Small”.

Q43. What does the if statement check in bash?


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).

Q44. Output of this snippet:


if echo "Hi"; then
echo "Yes"
fi

a) Hi​
b) Yes​


c) Hi​
Yes ​
d) Error​
e) None​
Explanation: echo always succeeds (exit code 0), so both lines print.

Q45. What will happen?


if grep "root" /etc/passwd; then
echo "User found"
else
echo "Not found"
fi

a) Always prints “User found” (on Linux)​
b) Always prints “Not found”​
c) Syntax error​
d) Prints both​
e) None​
Explanation: grep returns 0 if match is found → “User found”.

Q46. Identify correct syntax:

a) if[ $x -eq 1 ];then echo yes;fi​


b) if [ $x -eq 1 ]; then echo yes; fi ✅​
c) if $x==1 then echo yes fi​
d) if ($x -eq 1); then echo yes; fi​
e) if test $x eq 1 then echo yes fi​
Explanation: Proper spacing around [ ] is mandatory.

Q47. Output of:


a=4
b=2
if (( a / b == 2 )); then
echo "ok"
fi


a) Error​
b) ok ​
c) 2​
d) Division not supported​
e) None​
Explanation: Arithmetic expression ((a / b == 2)) evaluates true.

Q48. What is true about [ "$var" = "abc" ]?



a) Error if $var is empty ​
b) Always true​
c) Checks equality safely​
d) Always false​
e) Never executes​
Explanation: If $var is empty and unquoted, it breaks test syntax. Quoting prevents that.

Q49. Which of the following executes only if a command fails?

a) cmd && echo success​


b) cmd || echo failed ✅​
c) if cmd then echo fail​
d) !cmd && echo fail​
e) Both b and d​
Explanation: The || operator runs the right command only if the left one fails.

Q50. What will be the output?


x=5
if [ $x -eq 5 ]; then
echo $((x+5))
else
echo $(($x*2))
fi

a) 10 ✅​
b) 25​
c) 5​
d) $x+5​
e) Error​
Explanation: Condition true → prints arithmetic result 5+5=10.

⚙️ Shell Scripting – Set 3 (Q51–Q75)


(Topics: Loops + Return / Exit Status)
Q51. Which of the following is the correct syntax of a for loop in bash?

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.

Q52. Output of:


for i in 1 2 3; do
echo -n "$i "
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.

Q53. What will this print?


for ((i=1;i<=3;i++))
do
echo $((i*i))
done

✅​
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.

Q54. Which loop executes as long as the condition is true?

a) for​


b) until​
c) while ​
d) foreach​
e) repeat​
Explanation: while runs while condition evaluates true (exit code 0).

Q55. Identify valid syntax for a while loop:

a) while [ $x -lt 5 ]; do echo $x; done ✅​


b) while ($x < 5) { echo $x; }​
c) while $x < 5 then echo $x done​
d) while test $x<5 do echo $x done​
e) while do echo done​
Explanation: Bash uses [ condition ]; do …; done.

Q56. What is output?


x=1
while [ $x -le 3 ]
do
echo $x
x=$((x+1))
done


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.

Q58. What is the output?


n=1
until [ $n -gt 3 ]
do
echo $n
n=$((n+1))
done


a) 1 2​
b) 1 2 3 ​
c) None​
d) Infinite loop​
e) Error​
Explanation: until stops when $n > 3`.

Q59. What is output of:


for i in 1 2 3 4 5
do
if [ $i -eq 3 ]; then
break
fi
echo -n "$i "
done
a) 1 2 ✅ ​
b) 1 2 3 4 5​
c) 3 4 5​
d) Infinite​
e) Error​
Explanation: Loop breaks when i = 3.

Q60. What is output of:


for i in 1 2 3 4 5
do
if [ $i -eq 3 ]; then
continue
fi
echo -n "$i "
done


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.

Q61. What will happen?


for file in $(ls *.txt)
do
echo "$file"
done


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.

Q63. Identify correct loop that reads lines from a file:

a) for line in [Link]​


b) while read line; do echo $line; done < [Link] ✅​
c) while $line in [Link]​
d) loop read [Link]​
e) cat [Link] | while read line do echo done​
Explanation: Redirection < file feeds input to read.

Q64. What happens when read reaches EOF?


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

(Assume “admin” not found)​


a) 0​
b) 1 ​
c) 2​
d) 127​
e) 255​
Explanation: grep returns 1 if no match found.

Q69. What is the output?


false
echo $?


a) 0​
b) 1 ​
c) 2​
d) 255​
e) None​
Explanation: false always returns exit code 1.

Q70. Output of:


true
echo $?

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.

Q72. What is the difference between exit and return in bash?

✅​
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.

Q73. Output of:


myfun() { return 3; }
myfun
echo $?


a) 0​
b) 3 ​
c) 1​
d) 255​
e) Error​
Explanation: $? captures return status 3 from function.

Q74. What will be printed?


cmd() { ls /fakepath; echo "done"; }
cmd
echo $?

a) 0​


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.

Shell Scripting Basics

Q1. What is the default shell in most modern Linux distributions?​


a) /bin/csh​
b) /bin/sh​
c) /bin/bash ✅​
d) /bin/zsh​
e) /usr/bin/ksh​
Explanation: Bash (Bourne Again Shell) is the default shell in most Linux systems.

Q2. Which command is used to execute a shell script named [Link]?​


a) execute [Link]​
b) bash [Link] ✅​
c) run [Link]​
d) launch [Link]​
e) open [Link]​
Explanation: bash [Link] or ./[Link] executes the script.
Q3. Which line is known as the shebang in a shell script?​
a) //bin/bash​
b) #!bin/bash​
c) #!/bin/bash ✅​
d) ##/bin/bash​
e) $!/bin/bash​
Explanation: The shebang (#!) specifies the interpreter to run the script.


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.

Q5. What will this command print:

echo "Shell scripting is fun"

a) Nothing​
b) Shell scripting​
c) Shell scripting is fun ✅​
d) Error​
e) fun​
Explanation: echo simply outputs the given text.

Q6. Which of the following comments a line in bash?​


a) //​
b) --​
c) # ✅​
d) ;​
e) /* */​
Explanation: Lines beginning with # are comments in shell scripts.

Q7. What will echo $0 display when running a shell script?​


a) Last argument passed​


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.

Q12. Which file contains user-specific shell settings?​


a) /etc/bashrc​
b) ~/.bashrc ✅​
c) /etc/profile​
d) ~/.bash_profile​
e) /usr/bin/env​
Explanation: ~/.bashrc holds user-level configurations.

Q13. What will happen if you miss the shebang line?​


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.

Q14. Identify the correct statement:​


a) Shell script files must end with .bash​


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:

echo "Hello $USER"


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.

Q16. How do you correctly declare a variable in bash?​


a) x = 10​
b) $x = 10​
c) x=10 ✅​
d) int x=10​
e) declare(x=10)​
Explanation: Bash does not allow spaces around = when assigning values.

Q17. What is the output of the following?

x=10
echo $x

a) x=10​
b) $x​
c) 10 ✅ ​
d) Error​
e) Null​
Explanation: $x expands to the variable’s value.

Q18. What is the output of:

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.

Q20. What will happen if you execute:

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.

Q21. Which of the following converts a local variable to environment variable?​


a) declare x​
b) set x​
c) export x ✅​
d) env x​
e) public x​
Explanation: export makes a variable accessible to child processes.
Q22. What will this print?

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.

Q23. What is the output of:

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.

Q24. How can you remove a variable in shell?​


a) delete var​
b) unset var ✅​
c) remove var​
d) clear var​
e) set -r var​
Explanation: unset deletes a variable from the shell environment.

Q25. What is the output of:

name="Harsh"
echo "Hello $name"

a) $name​
b) Hello Harsh ✅​
c) Hello $name​
d) Harsh​
e) None​
Explanation: $name expands to its stored value.

Q26. What is the result of:

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.

Q28. What is the output?

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.

Q30. What is the output of:

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.

Q32. What does $# represent?​


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.

Q33. What does $0 contain?​


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.

Q34. What will this script print when executed as ./[Link] 2 3?

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 $*

when the script is run as ./[Link] A B C?​


a) A​
b) B C​
c) A B C ✅​
d) $*​
e) Error​
Explanation: $* expands to all arguments as a single word.

Q36. What is the output of:

echo "$@"

when run as ./[Link] A B C?​


a) A B C ✅​
b) A​
c) A, B, C​
d) $@​
e) Error​
Explanation: $@ expands each argument as a separate word.

Q37. What will this script print?

echo "$# arguments were passed"

if run as ./[Link] one two three​


a) 3 arguments were passed ✅​
b) 2 arguments were passed​
c) 0 arguments were passed​
d) one two three arguments were passed​
e) Error​
Explanation: $# = 3 → total arguments.
Q38. What happens if you reference $3 when only one argument was passed?​
a) Error​
b) Prints $3​
c) Prints blank ✅

d) Prints 0​
e) Prints NULL​
Explanation: Undefined positional parameters expand to empty string.

Q39. What does the shift command do in bash?​


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.

Q40. What will this script output?

echo $1
shift
echo $1

when run as ./[Link] A B C​


a) A B ✅​
b) B C​
c) A A​
d) A​
e) B​
Explanation: After shift, $2 becomes $1, so prints A then B.

Q41. What is printed by:

echo "$@"
shift 2
echo "$@"

if run as ./[Link] one two three four​


a) one two three four → three four ✅​
b) one two → three four​
c) Error​
d) three four → one two​
e) None​
Explanation: shift 2 discards first two arguments.

Q42. What is the output of:

for arg in "$@"; do echo $arg; done

when run as ./[Link] red blue green​


a) red blue green​
b) red, blue, green printed separately ✅​
c) $@​
d) Error​
e) None​
Explanation: Iterates through all arguments.


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.

Q44. What will this print?

echo "The last argument is ${!#}"


when run as ./[Link] 10 20 30​
a) ${!#}​
b) 3​
c) 30 ✅​
d) Error​
e) $#​
Explanation: ${!#} expands to the last positional argument.

Q45. What is the output of:

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.

Q47. Which command checks if a file named [Link] exists?​


a) if [ -d [Link] ]​
b) if [ -f [Link] ]​
c) if [ -e [Link] ] ✅​
d) if [ -x [Link] ]​
e) if [ [Link] ]​
Explanation: -e returns true if the file exists.

Q48. What is the output of:

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.

Q49. Which operator checks if two strings are equal?​


a) -eq​
b) = ✅​
c) ==​
d) -s​
e) -str​
Explanation: String comparison uses = inside [ ].

Q50. What is the output?

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.

Q51. What happens if spaces are missing in [ $x==5 ]?​


a) Error ✅ ​
b) Works fine​
c) Prints blank​
d) Ignores​
e) Prints $x==5​
Explanation: [ ] must have spaces before and after condition tokens.

Q52. What does this test return true for?

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.

Q53. Which condition checks if variable $num is greater than 10?​


a) [ $num > 10 ]​
b) [ $num -gt 10 ] ✅​
c) [ $num >= 10 ]​
d) [ $num -ge 10 ]​
e) [ $num gtr 10 ]​
Explanation: -gt is used for numeric greater than comparison.
Q54. What is the output?

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.

Q55. How to represent OR condition in bash if?​


a) || ✅​
b) |​
c) or​
d) /​
e) -o​
Explanation: In [[ ]], || performs logical OR.

Q56. What will this print?

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.

Q57. Which condition checks if a file [Link] is executable?​


a) [ -e [Link] ]​
b) [ -f [Link] ]​
c) [ -x [Link] ] ✅​
d) [ -r [Link] ]​
e) [ -s [Link] ]​
Explanation: -x checks for execute permission.

Q58. What is the output of:

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.

Q59. What will happen if fi is missing in a bash script?​


a) Nothing​
b) Syntax error ​
c) Condition skips​
d) Prints 0​
e) Default fi assumed​
Explanation: Every if must end with fi.

Q60. What is the output?

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.

Q26. What will be the output of the following script?


#!/bin/bash
a=10
b=20
echo $((a==b))

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.

Q27. What is the output of the following?


#!/bin/bash
echo $(($2 + $3))

Command executed: ./[Link] 3 7 2​


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 )).

Q28. How do you make a variable available to child processes?

a) Use read var​


b) Use local var​
c) Use export var ✅​
d) Use declare var​
e) Use source var

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

Q30. What will be the output of the following script?


#!/bin/bash
x=5
y=10
if [ $x -eq 5 -a $y -eq 10 ]; then
echo "True"
else
echo "False"
fi

✅​
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 ]

Q32. What will happen if you execute this command?


echo $((5/0))

a) 0​

✅​
b) Infinity​
c) Division by zero error
d) Nothing​
e) 5

Explanation: Division by zero is not allowed — bash prints an error.

Q33. What is the default exit status of a successfully executed command?

a) 1​
b) -1​


c) 255​
d) 0 ​
e) None

Q34. What is the output of the following script?


#!/bin/bash
for i in {3..7}
do
echo -n "$i "
done

a) 3 4 5 6 7✅ ​
b) 1 2 3 4 5​
c) 7 6 5 4 3​
d) 3..7​
e) Error

Q35. What is the correct syntax for an infinite loop in bash?

a) while (true)​
b) while true; do ... done ✅​
c) loop forever​
d) for(;;)​
e) repeat do done

Q36. What is the effect of the shift command in a shell script?


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.

Q37. What will the following script print?


#!/bin/bash
a=3
b=7
if ((a<b)); then
echo $((b-a))
fi

a) 4 ✅​
b) 10​
c) 3​
d) 7​
e) None

Q38. Which of the following symbols is used for command substitution?

a) #​
b) $() ✅​
c) @()​
d) []​
e) {}

Q39. What will be printed?


#!/bin/bash
x=8
y=3
echo $((x%y))

a) 1 ✅​
b) 2​
c) 0​
d) 8​
e) Error

Q40. Which of the following is used to check if a string is empty?

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

Q42. What happens if you forget fi in an if statement?


a) Script runs normally​
b) Syntax error ​
c) It ignores condition​
d) fi is optional​
e) None

Q43. What will be printed?


#!/bin/bash
count=0
while [ $count -lt 3 ]
do
echo $count
((count++))
done

a) 1 2 3​
b) 0 1 2 ​
c) 0 1 2 3​
d) 1 2​
e) Error

Q44. What is the meaning of $# in a shell script?


a) Number of background processes​
b) Number of arguments passed ​
c) Process ID of shell​
d) The exit code​
e) None

Q45. What is the difference between $* and $@?

a) No difference​
b) $* preserves spaces; $@ doesn’t​
c) $@ treats each argument separately ✅​
d) $* gives total count​
e) Both expand only first argument

Q46. What is the output of the following code?


#!/bin/bash
for i in 1 2 3; do
echo -n "$i "
done

a) 1 2 3 ✅​
b) 123​
c) 1\n2\n3​
d) Error​
e) None

Q47. What is the output of this script?


#!/bin/bash
for i in {1..5..2}
do
echo -n "$i "
done

a) 1 2 3 4 5​
b) 1 3 5 ✅​
c) 2 4​
d) Error​
e) None

Explanation: The {1..5..2} syntax increments by 2.

Q48. What happens when this script executes?


#!/bin/bash
for file in *.txt
do
echo $file
done

a) Lists only .txt files ✅​


b) Deletes .txt files​
c) Renames files​
d) Opens .txt files​
e) Error

Q49. What is the output?


#!/bin/bash
x=5
y=10
if (( x+y > 12 )); then
echo "Yes"
else
echo "No"
fi

a) Yes ✅ ​
b) No​
c) 15​
d) Error​
e) None

Q50. What is the return value of a successful command in shell scripting?

a) 0 ✅ ​
b) 1​
c) 255​
d) -1​
e) None

Q51. What is the output of the following?


#!/bin/bash
grep "root" /etc/passwd >/dev/null
echo $?

a) Prints 0 if “root” found ✅​


b) Always prints 1​
c) Prints username​
d) Prints nothing​
e) Error

Q52. What does exit 5 mean inside a shell script?

a) Terminates script and returns 5 as exit code ✅​


b) Prints 5​
c) Ignores exit​
d) Terminates but no return​
e) Returns true
Q53. What will happen in this script?
#!/bin/bash
function sum() {
return $(($1+$2))
}
sum 5 10
echo $?


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.

Q54. What is the correct way to capture the output of a command in a


variable?

a) var = cmd​
b) var=$(cmd) ✅​
c) var=cmd​
d) $var=cmd​
e) var => cmd

Q55. Which loop syntax is valid in bash?

a) for ((i=0; i<5; i++)) ✅​


b) for (i=0; i<5; i++)​
c) loop(i<5)​
d) do for i​
e) None

Q56. What will this print?


#!/bin/bash
for ((i=2; i<=6; i+=2))
do
echo -n "$i "
done

a) 2 4 6 ✅​
b) 2 3 4 5 6​
c) 2 4​
d) 6 4 2​
e) None

Q57. What is printed by this script?


#!/bin/bash
for i in $(seq 3)
do
echo -n "$i "
done

a) 0 1 2​
b) 1 2 3 ✅​
c) seq 3​
d) Error​
e) None

Q58. What does this script output?


#!/bin/bash
for word in $(echo "IFSCA Exam IT Stream")
do
echo $word
done

✅​
a) Prints the full line​
b) Prints each word on a new line
c) Error​
d) Prints only “IFSCA”​
e) None

Q59. What is the effect of break inside a loop?

✅​
a) Skips next iteration​
b) Stops loop execution
c) Jumps to top​
d) Restarts loop​
e) None

Q60. What does continue do in a loop?

✅​
a) Exits loop​
b) Skips current iteration
c) Ends script​
d) Restarts program​
e) None

Q61. What will be the output?


#!/bin/bash
i=1
while [ $i -le 3 ]
do
echo $i
((i++))
done

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

Q63. Which operator performs integer comparison for equality in bash?

a) ==​
b) -eq ✅​
c) =​
d) is​
e) equals

Q64. What is the correct way to check if a directory exists?

a) [ -d /path/to/dir ] ✅​
b) [ -f /path/to/dir ]​
c) [ dir /path ]​
d) [ -e file ]​
e) [ -x path ]

Q65. What is the output?


#!/bin/bash
arr=(10 20 30)
echo ${arr[1]}


a) 10​
b) 20 ​
c) 30​
d) 0​
e) Error

Q66. What does $? represent?

✅​
a) The PID​
b) The exit status of the last command
c) Total number of arguments​
d) Shell version​
e) None

Q67. What does set -e do in a shell script?

✅​
a) Ignores errors​
b) Exits immediately if a command fails
c) Enables echoing​
d) Prints environment​
e) None

Q68. What will this print?


#!/bin/bash
var=10
((var+=5))
echo $var

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

Q70. What is printed?


#!/bin/bash
x=3
y=2
echo $(($x**$y))


a) 6​
b) 9 ​
c) 8​
d) Error​
e) None

Q71. What will the following command output?


echo $(echo 2+3 | bc)

a) 2+3​
b) 5 ✅​
c) bc​
d) Error​
e) None

Explanation: bc evaluates arithmetic expressions, so 2+3 → 5.

Q72. What will be the output?


echo $($2+$3)

Command: ./[Link] 4 5 6​
a) 11​
b) Error ✅​
c) 9​
d) 56​
e) None

Explanation: $($2+$3) tries to execute the command 5+6, which is invalid.​


Correct syntax: $(( $2 + $3 )).

Q73. Which of the following correctly prints the number of arguments


passed to a script?

a) echo $# ✅​
b) echo $@​
c) echo $*​
d) echo $$​
e) echo $?

Q74. What will be the output?


#!/bin/bash
echo "Arguments: $*"
echo "Count: $#"
Command: ./[Link] A B "C D"​


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

Explanation: Quoted string "C D" counts as one argument.

Q75. What is the output of this line?


echo "File count: $(ls | wc -l)"

✅​
a) Prints names of files​
b) Prints total number of files
c) Prints all file sizes​
d) Syntax error​
e) None

Q76. What does this command print?


echo "Total: $(expr 3 + 4)"

a) Total: 7 ✅​
b) Total: expr 3 + 4​
c) Error​
d) Total:​
e) None

Q77. What is the output?


#!/bin/bash
a=4
b=2
echo "$((a*b + a/b))"
a) 10✅ ​
b) 8​
c) 6​
d) Error​
e) 2

Q78. What does the following command do?


grep -c "bash" /etc/passwd

✅​
a) Prints lines containing bash​
b) Counts lines containing “bash”
c) Deletes lines containing “bash”​
d) Replaces “bash”​
e) None

Q79. What is the function of this command?


cut -d ':' -f1 /etc/passwd

a) Shows first field (usernames) ✅​


b) Shows all lines​
c) Deletes first field​
d) Replaces : with ,​
e) None

Q80. What will this command print?


awk -F: '{print $1,$3}' /etc/passwd | head -n 2

✅​
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) Number of “root” lines ✅​


b) Prints “root”​
c) Error​
d) 0 always​
e) None

Q82. What is the output?


echo $(pwd)

a) Prints current directory ✅​


b) Prints shell name​
c) Prints home directory​
d) Error​
e) None

Q83. What will find /home -name "*.sh" do?

a) Delete .sh files​


b) List .sh files ✅​
c) Run .sh files​
d) Count .sh files​
e) None

Q84. What happens with this script?


#!/bin/bash
if [ -f "$1" ]; then
echo "File exists"
else
echo "Not found"
fi

Command: ./[Link] [Link]​

✅​
a) Always prints “File exists”​
b) Prints “File exists” if [Link] present
c) Prints “Not found” always​
d) Error​
e) None

Q85. What does basename /home/user/[Link] output?

a) /home/user​
b) [Link] ✅​
c) /home​
d) None​
e) user

Q86. What is printed?


#!/bin/bash
x=$(date +%Y)
echo "Year: $x"

✅​
a) Current date​
b) Current year
c) 2025 only​
d) Error​
e) None

Q87. What happens here?


#!/bin/bash
read -p "Enter name: " name
echo "Welcome, $name!"
a) Takes input and prints greeting ✅​
b) Error​
c) Reads file​
d) No input​
e) None

Q88. What is the output?


#!/bin/bash
for i in $(ls *.sh); do
echo "Script: $i"
done

a) Prints .sh filenames ✅​


b) Runs each .sh file​
c) Deletes .sh files​
d) Error​
e) None

Q89. What happens if you run this?


#!/bin/bash
echo $(($1 * $2))

Command: ./[Link] 3 5​


a) 8​
b) 15 ​
c) 35​
d) Error​
e) None

Q90. What does $0 represent?


a) Last argument​
b) Script name ​
c) First argument​
d) Exit code​
e) None

Q91. Which command replaces “foo” with “bar” in [Link]?

a) awk '{gsub("foo","bar")}' [Link] ✅​


b) grep foo bar [Link]​
c) find foo bar​
d) ls foo bar​
e) None

Q92. What does this line print?


echo $(echo $(echo Hello))

a) Hello✅ ​
b) echo Hello​
c) Error​
d) None​
e) Hello Hello

Q93. What does this script print?


#!/bin/bash
n=1
while [ $n -le 3 ]; do
echo "Loop $n"
((n++))
done

a) Loop 1 Loop 2 Loop 3 ✅​


b) Infinite loop​
c) 1 2 3​
d) Error​
e) None
Q94. What happens here?
#!/bin/bash
for ((i=1; i<=3; i++))
do
[ $i -eq 2 ] && continue
echo $i
done


a) 1 2 3​
b) 1 3 ​
c) 2 3​
d) 1 2​
e) None

Q95. What will the output be?


#!/bin/bash
arr=(A B C)
for i in "${arr[@]}"; do
echo -n "$i "
done

a) A B C ✅​
b) ABC​
c) Array​
d) None​
e) Error

Q96. What does $(whoami) print?

✅​
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

Q98. What will this output?


#!/bin/bash
x="IFSCA"
y="Exam"
echo "${x}_${y}"

✅​
a) IFSCAExam​
b) IFSCA_Exam
c) _Exam​
d) Error​
e) None

Q99. Which command shows total number of users in the system?

a) cat /etc/passwd | wc -l ✅​
b) ls /home | wc​
c) who​
d) id​
e) grep users

Q100. What will this print?


#!/bin/bash
x=10
y=20
echo "$(($x<$y?1:0))"

a) 0​
b) 1 ​
c) 10​
d) 20​
e) Error

You might also like