FahmidasClassroom

Learn by easy steps

Bash

1. Which command is used to list files in the current directory?

– a) ls

– b) list

– c) dir

– d) show

– Answer: a) ls

2. How do you display the current working directory?

– a) pwd

– b) cwd

– c) dir

– d) ls -d

– Answer: a) pwd

3. Which command is used to create a new directory?

– a) newdir

– b) makedir

– c) mkdir

– d) createdir

– Answer: c) mkdir

4. How do you remove a file in Bash?

– a) del

– b) rm

– c) erase

– d) remove

– Answer: b) rm

5. Which command is used to copy files?

– a) cp

– b) copy

– c) mv

– d) duplicate

– Answer: a) cp

6. What is the command to move or rename files?

– a) mv

– b) move

– c) rename

– d) cp -m

– Answer: a) mv

7. How do you display the content of a file?

– a) display

– b) show

– c) cat

– d) view

– Answer: c) cat

8. What command shows the last few lines of a file?

– a) tail

– b) end

– c) bottom

– d) last

– Answer: a) tail

9. Which command is used to view the beginning of a file?

– a) head

– b) start

– c) top

– d) begin

– Answer: a) head*

10. How do you search for a specific pattern in a file?

– a) search

– b) find

– c) grep

– d) look

– Answer: c) grep

11. What command is used to change file permissions?

– a) chmod

– b) chperm

– c) chown

– d) perm

– Answer: a) chmod

12. Which command is used to change the owner of a file?

– a) chown

– b) chperm

– c) chmod

– d) owner

– Answer: a) chown*

13. How can you display disk usage of files and directories?

– a) du

– b) df

– c) diskusage

– d) disku

– Answer: a) du*

14. Which command can be used to find the location of an executable?

– a) which

– b) where

– c) locate

– d) find

– Answer: a) which

15. Which command is used to display or set the system’s hostname?

– a) hostname

– b) sysname

– c) hostconfig

– d) namesys

– Answer: a) hostname

16. What is the command to check the current user’s username?

– a) whoami

– b) username

– c) user

– d) me

– Answer: a) whoami

17. How do you search for files in a directory hierarchy?

– a) find

– b) search

– c) locate

– d) searchfile

– Answer: a) find

18. What command is used to concatenate and display files?

– a) cat

– b) concat

– c) join

– d) display

– Answer: a) cat

19. How do you search for a file in the current directory?

– a) ls | grep filename

– b) find . -name filename

– c) search filename

– d) locate filename

– Answer: b) find . -name filename

20. Which command lists the files with detailed information?

– a) ls -l

– b) ls -d

– c) list -d

– d) lsd

– Answer: a) ls -l

21. How do you define a variable in Bash?

– a) var name=value

– b) variable name=value

– c) name=value

– d) define name=value

– Answer: c) name=value

22. How do you access the value of a variable named `myvar`?

– a) $myvar

– b) myvar$

– c) ${myvar}

– d) both a and c

– Answer: d) both a and c

23. Which command lists all environment variables?

– a) env

– b) set

– c) export

– d) listenv

– Answer: a) env

24. How do you make a variable available to sub-shells?

– a) declare myvar

– b) global myvar

– c) export myvar

– d) env myvar

– Answer: c) export myvar

25. What is the correct syntax to assign the output of a command to a variable?

– a) var=$(command)

– b) var=command

– c) var={command}

– d) var=exec command

– Answer: a) var=$(command)

26. Which of the following is used to remove a variable in Bash?

– a) delete varname

– b) remove varname

– c) unset varname

– d) clear varname

– Answer: c) unset varname

27. How do you create a read-only variable in Bash?

– a) readonly varname

– b) const varname

– c) declare -r varname

– d) both a and c

– Answer: d) both a and c

28. What is the default value of an uninitialized variable in Bash?

– a) null

– b) undefined

– c) empty string

– d) 0

– Answer: c) empty string

29. How can you append text to a variable in Bash?

– a) var=$var”text”

– b) var=${var}text

– c) var+=”text”

– d) both b and c

– Answer: d) both b and c

30. How do you check if a variable named `var` is set?

– a) [ -z “$var” ]

– b) [ -n “$var” ]

– c) [ ! -z “$var” ]

– d) [ -s “$var” ]

– Answer: b) [ -n “$var” ]

31. Which of the following is the correct syntax for an if-else statement in Bash?

– a) if [ condition ] then; commands; fi

– b) if [ condition ]; then commands; else commands; fi

– c) if condition; then commands; else commands; end

– d) if (condition); then commands; else commands; fi

– Answer: b) if [ condition ]; then commands; else commands; fi

32. How do you check if a file exists in Bash?

– a) if [ -e filename ]

– b) if [ -f filename ]

– c) if [ -d filename ]

– d) if [ -x filename ]

– Answer: a) if [ -e filename ]

33. Which operator is used to check if two strings are equal in Bash?

– a) ==

– b) =

– c) -eq

– d) -is

– Answer: b) =

34. How do you check if a variable `var` is not empty in Bash?

– a) if [ -z “$var” ]

– b) if [ -n “$var” ]

– c) if [ ! -z “$var” ]

– d) if [ “$var” != “” ]

– Answer: b) if [ -n “$var” ]

35. Which of the following is used to check if a number is greater than another in Bash?

– a) if [ num1 > num2 ]

– b) if [ num1 -gt num2 ]

– c) if (( num1 > num2 ))

– d) both b and c

– Answer: d) both b and c

36. What is the correct syntax for an if-elif-else statement in Bash?

– a) if [ condition1 ]; then commands; elif [ condition2 ]; then commands; else commands; fi

– b) if [ condition1 ] then commands elif [ condition2 ] then commands else commands fi

– c) if (condition1) then commands; elif (condition2) then commands; else commands; fi

– d) if [ condition1 ]; then commands; else if [ condition2 ]; then commands; else commands; fi

– Answer: a) if [ condition1 ]; then commands; elif [ condition2 ]; then commands; else commands; fi

37. How do you negate a condition in Bash?

– a) if ![ condition ]

– b) if [ not condition ]

– c) if [ ! condition ]

– d) if [ -not condition ]

– Answer: c) if [ ! condition ]

38. Which operator is used to check if a variable is equal to a specific value in a Bash arithmetic condition?

– a) -eq

– b) =

– c) ==

– d) -is

– Answer: a) -eq

39. How do you combine multiple conditions in a single if statement in Bash?

– a) if [ condition1 ] && [ condition2 ]

– b) if [ condition1 ] || [ condition2 ]

– c) if [[ condition1 && condition2 ]]

– d) both a and c

– Answer: d) both a and c

40. Which of the following checks if a directory exists in Bash?

– a) if [ -e directory ]

– b) if [ -d directory ]

– c) if [ -f directory ]

– d) if [ -x directory ]

– Answer: b) if [ -d directory ]*

41. Which of the following is the correct syntax for a for loop in Bash?

– a) for item in list; do commands; done

– b) for (item in list) { commands; }

– c) foreach item in list { commands; }

– d) loop item in list { commands; }

– Answer: a) for item in list; do commands; done

42. How do you create a while loop in Bash?

– a) while condition do commands done

– b) while (condition) { commands }

– c) while [ condition ]; do commands; done

– d) while [ condition ]: do commands done

– Answer: c) while [ condition ]; do commands; done

43. Which keyword is used to exit a loop early in Bash?

– a) exit

– b) break

– c) stop

– d) continue

– Answer: b) break

44. How do you skip the current iteration and continue with the next iteration of a loop in Bash?

– a) skip

– b) continue

– c) next

– d) pass

– Answer: b) continue

45. What is the output of the following Bash script?

“`bash

For I in 1 2 3; do

Echo $i

Done

“`

– a) 123

– b) 1 2 3

– c) 1

2

3

– d) Error

– Answer: c) 1

2

3

46. Which of the following is the correct way to write an infinite loop in Bash?

– a) while [ true ]; do commands; done

– b) while true; do commands; done

– c) for (( ; ; )); do commands; done

– d) all of the above

– Answer: d) all of the above

47. How do you write a C-style for loop in Bash?

– a) for (i=0; i<n; i++); do commands; done

– b) for I in 0..n; do commands; done

– c) for (( i=0; i<n; i++ )); do commands; done

– d) for I from 0 to n; do commands; done

– Answer: c) for (( i=0; i<n; i++ )); do commands; done

48. Which of the following can be used to iterate over the lines of a file in Bash?

– a) for line in $(cat file); do commands; done

– b) while read line; do commands; done < file

– c) for line in `cat file`; do commands; done

– d) both b and c

– Answer: d) both b and c

49. What is the output of the following Bash script?

“`bash

For I in {1..5}; do

If [ $i -eq 3 ]; then

Break

Fi

Echo $i

Done

“`

– a) 1 2 3

– b) 1 2

– c) 1 2 3 4 5

– d) 3 4 5

– Answer: b) 1 2

50. How do you iterate over a range of numbers from 1 to 10 in steps of 2 in Bash?

– a) for I in {1..10..2}; do commands; done

– b) for (( i=1; i<=10; i+=2 )); do commands; done

– c) for I in $(seq 1 2 10); do commands; done

– d) both b and c

– Answer: d) both b and c

51. How do you access the first command line argument in a Bash script?

– a) $0

– b) $1

– c) $@

– d) $#

– Answer: b) $1

52. Which variable contains the name of the script itself?

– a) $0

– b) $1

– c) $2

– d) $*

– Answer: a) $0

53. How do you access all command line arguments passed to a Bash script?

– a) $*

– b) $@

– c) both a and b

– d) $#

– Answer: c) both a and b

54. Which variable holds the number of command line arguments passed to a script?

– a) $0

– b) $*

– c) $@

– d) $#

– Answer: d) $#

55. What is the output of the following script if run as `./script.sh arg1 arg2`?

“`bash

Echo $2

“`

– a) arg1

– b) arg2

– c) script.sh

– d) Error

– Answer: b) arg2

56. Which of the following is used to loop through all command line arguments in a Bash script?

– a) for arg in $*; do echo $arg; done

– b) for arg in $@; do echo $arg; done

– c) for arg in $@; do echo $1; done

– d) both a and b

– Answer: d) both a and b

57. What is the result of the following script if run as `./script.sh “arg 1” arg2`?

“`bash

For arg in “$@”; do echo $arg; done

“`


  • A) arg

1

Arg2


  • B) “arg 1”

Arg2


  • C) arg 1

Arg2

– d) Error

– Answer: c) arg 1

Arg2

58. How do you check if at least one command line argument was passed to a script?

– a) if [ $# -gt 0 ]

– b) if [ $* -ne 0 ]

– c) if [ $# -ge 1 ]

– d) both a and c

– Answer: d) both a and c

59. What is the output of the following script if no arguments are passed?

“`bash

Echo $1

“`

– a) 0

– b) 1

– c) nothing (empty output)

– d) Error

– Answer: c) nothing (empty output)

60. Which special parameter is used to shift the positional parameters to the left in a Bash script?

– a) move

– b) shift

– c) pop

– d) push

– Answer: b) shift

61. Which command is used to read a file line by line in Bash?

– a) while read line; do commands; done < filename

– b) for line in $(cat filename); do commands; done

– c) readfile filename

– d) read < filename

– Answer: a) while read line; do commands; done < filename

62. What is the correct syntax to write “Hello World” to a file named `output.txt`?

– a) echo “Hello World” > output.txt

– b) write “Hello World” > output.txt

– c) cat “Hello World” > output.txt

– d) print “Hello World” > output.txt

– Answer: a) echo “Hello World” > output.txt

63. How do you append “Goodbye World” to the end of `output.txt` without overwriting it?

– a) echo “Goodbye World” > output.txt

– b) echo “Goodbye World” >> output.txt

– c) cat “Goodbye World” >> output.txt

– d) append “Goodbye World” >> output.txt

– Answer: b) echo “Goodbye World” >> output.txt

64. Which command can be used to display the content of a file?

– a) cat

– b) show

– c) read

– d) display

– Answer: a) cat

65. What is the output of the following script if `input.txt` contains three lines: “line1”, “line2”, “line3”?

“`bash

While IFS= read -r line; do

Echo $line

Done < input.txt

“`

– a) line1 line2 line3

– b) line1

Line2

Line3

– c) “line1” “line2” “line3”

– d) Error

– Answer: b) line1

Line2

Line3

66. How do you count the number of lines in a file named `file.txt`?

– a) wc -l file.txt

– b) count -l file.txt

– c) lines file.txt

– d) nl file.txt

– Answer: a) wc -l file.txt

67. Which command is used to read the first 10 lines of a file?

– a) head file.txt

– b) first file.txt

– c) top file.txt

– d) read -n 10 file.txt

– Answer: a) head file.txt

68. How do you read the last 10 lines of a file?

– a) tail file.txt

– b) last file.txt

– c) end file.txt

– d) read -n -10 file.txt

– Answer: a) tail file.txt

69. Which command can be used to create an empty file if it does not exist, or update the timestamp if it does exist?

– a) touch filename

– b) create filename

– c) update filename

– d) timestamp filename

– Answer: a) touch filename

70. What does the following command do?

“`bash

Cat file1.txt file2.txt > merged.txt

“`

– a) Displays the contents of both file1.txt and file2.txt

– b) Merges the contents of file1.txt and file2.txt into merged.txt

– c) Appends the contents of file2.txt to file1.txt

– d) Copies the contents of file1.txt to file2.txt

– Answer: b) Merges the contents of file1.txt and file2.txt into merged.txt

71. Which of the following is the correct syntax for command substitution in Bash?

– a) `$(command)`

– b) `command`

– c) `${command}`

– d) `{{command}}`

– Answer: a) `$(command)`

72. What is the output of the following script?

“`bash

Echo “Today is $(date +%A)”

“`

– a) Today is (date +%A)

– b) Today is date +%A

– c) Today is Monday

– d) Today is (the actual day of the week)

– Answer: d) Today is (the actual day of the week)

73. How do you capture the output of a command into a variable in Bash?

– a) var=$(command)

– b) var=`command`

– c) var=${command}

– d) both a and b

– Answer: d) both a and b

74. Which substitution syntax is used to get the length of a variable in Bash?

– a) ${#var}

– b) ${var#}

– c) $(#var)

– d) $(var#)

– Answer: a) ${#var}

75. What does the following substitution do?

“`bash

${var:=default}

“`

– a) Sets var to “default” if var is unset or null, and returns the value of var

– b) Always sets var to “default”

– c) Checks if var is set and returns “default” if it is

– d) Sets var to “default” and returns nothing

– Answer: a) Sets var to “default” if var is unset or null, and returns the value of var

76. Which substitution removes the shortest match of a pattern from the start of a variable’s value?

– a) ${var#pattern}

– b) ${var##pattern}

– c) ${var%pattern}

– d) ${var%%pattern}

– Answer: a) ${var#pattern}

77. How do you perform arithmetic substitution in Bash?

– a) $((expression))

– b) ${expression}

– c) $(expression)

– d) ((expression))

– Answer: a) $((expression))

78. What does `${var:-default}` do in Bash?

– a) Sets var to “default”

– b) Uses “default” if var is unset or null, without changing var

– c) Uses “default” if var is set, without changing var

– d) Appends “default” to var

– Answer: b) Uses “default” if var is unset or null, without changing var

79. How do you substitute all occurrences of a substring within a variable in Bash?

– a) ${var//pattern/replacement}

– b) ${var/pattern/replacement}

– c) ${var%%pattern/replacement}

– d) ${var##pattern/replacement}

– Answer: a) ${var//pattern/replacement}

80. What does `${var^}` do in Bash?

– a) Converts all characters in var to uppercase

– b) Converts the first character of var to uppercase

– c) Converts the first character of var to lowercase

– d) Converts all characters in var to lowercase

– Answer: b) Converts the first character of var to uppercase

81. What does the `grep` command do?

– a) Prints files

– b) Searches for patterns in files

– c) Deletes files

– d) Copies files

– Answer: b) Searches for patterns in files

82. Which option with `grep` makes the search case-insensitive?

– a) -v

– b) -i

– c) -c

– d) -n

– Answer: b) -i

83. How do you search for the word “apple” in the file `fruits.txt` using `grep`?

– a) grep fruits.txt apple

– b) grep apple

– c) grep apple fruits.txt

– d) grep “apple” fruits.txt

– Answer: c) grep apple fruits.txt

84. Which option with `grep` counts the number of matching lines?

– a) -n

– b) -c

– c) -l

– d) -o

– Answer: b) -c

85. What does `grep -v pattern file` do?

– a) Searches for lines that contain “pattern”

– b) Searches for lines that do not contain “pattern”

– c) Counts lines that contain “pattern”

– d) Prints the line numbers of matches

– Answer: b) Searches for lines that do not contain “pattern”

86. How do you search recursively in all files in a directory for the word “example” using `grep`?

– a) grep -I example

– b) grep -r example

– c) grep -R example

– d) both b and c

– Answer: d) both b and c

87. Which `grep` option displays the line numbers of matching lines?

– a) -l

– b) -n

– c) -o

– d) -b

– Answer: b) -n

88. How do you use `grep` to print only the names of files containing a match?

– a) grep -n pattern file

– b) grep -c pattern file

– c) grep -l pattern file

– d) grep -v pattern file

– Answer: c) grep -l pattern file

89. What is the purpose of the `-E` option in `grep`?

– a) Enables extended regular expressions

– b) Makes the search case-insensitive

– c) Displays the line number

– d) Searches recursively

– Answer: a) Enables extended regular expressions

90. What does `grep -w pattern file` do?

– a) Searches for lines containing the exact word “pattern”

– b) Searches for lines containing the pattern as part of a word

– c) Searches for lines that do not contain “pattern”

– d) Searches for lines starting with “pattern”

– Answer: a) Searches for lines containing the exact word “pattern”


  1. What does the following script output if `var` is set to “Hello”?

“`bash

Var=”Hello”

Echo “${var} World”

“`

– a) Hello World

– b) var World

– c) Hello

– d) Error

– Answer: a) Hello World


  1. What is the output of the following script if run with `./script.sh arg1 arg2`?

“`bash

Echo “First argument: $1”

Echo “Second argument: $2”

“`


  • A) First argument: arg2

Second argument: arg1


  • B) First argument: script.sh

Second argument: arg1


  • C) First argument: arg1

Second argument: arg2

– d) Error

– Answer: c) First argument: arg1

Second argument: arg2


  1. What will the following script print if the file `data.txt` contains “apple banana cherry”?

“`bash

While read -r fruit; do

Echo “Fruit: $fruit”

Done < data.txt

“`

– a) Fruit: apple banana cherry

– b) Fruit: apple

Fruit: banana

Fruit: cherry

– c) Fruit: apple

– d) Fruit: apple banana cherry

– Answer: c) Fruit: apple


  1. What is the result of the following script?

“`bash

For I in {1..3}; do

Echo “Number: $i”

Done

“`

– a) Number: 123

– b) Number: 1 Number: 2 Number: 3

– c) Number: 1

Number: 2

Number: 3

– d) Error

– Answer: c) Number: 1

Number: 2

Number: 3


  1. What will the following script output if `num` is 5?

“`bash

Num=5

If [ $num -gt 3 ]; then

Echo “Greater than 3”

Else

Echo “Less than or equal to 3”

Fi

“`

– a) Greater than 3

– b) Less than or equal to 3

– c) Error

– d) 5

– Answer: a) Greater than 3


  1. What does the following script do?

“`bash

Echo “Enter your name:”

Read name

Echo “Hello, $name”

“`

– a) Asks the user to enter their name and prints “Hello, [name]”

– b) Prints “Enter your name:” and exits

– c) Prints “Hello, name”

– d) Prints “Enter your name: $name”

– Answer: a) Asks the user to enter their name and prints “Hello, [name]”


  1. What is the output of the following script if `./script.sh Hello` is executed?

“`bash

Echo “Script name: $0”

Echo “First argument: $1”

“`


  • A) Script name: ./script.sh

First argument: Hello


  • B) Script name: Hello

First argument: ./script.sh


  • C) Script name: script.sh

First argument: Hello

– d) Error

– Answer: a) Script name: ./script.sh

First argument: Hello


  1. What will the following script output if executed without any command-line arguments?

“`bash

If [ $# -eq 0 ]; then

Echo “No arguments provided”

Else

Echo “Arguments provided”

Fi

“`

– a) Arguments provided

– b) No arguments provided

– c) Error

– d) $# -eq 0

– Answer: b) No arguments provided


  1. What is the effect of the following script on `output.txt`?

“`bash

Echo “Line 1” > output.txt

Echo “Line 2” >> output.txt

“`

– a) Overwrites `output.txt` with “Line 1” and then with “Line 2”

– b) Appends “Line 1” and “Line 2” to `output.txt`

– c) Writes “Line 1” and then appends “Line 2” to `output.txt`

– d) Deletes `output.txt`

– Answer: c) Writes “Line 1” and then appends “Line 2” to `output.txt