Bash MCQs

Bash MCQs

These Bash multiple-choice questions and their answers will help you strengthen your grip on the subject of Bash. You can prepare for an upcoming exam or job interview with these 50+ Bash MCQs.
So scroll down and start answering.

1: To run a copy command in a subshell, which syntax would you use?

A.   ( command )

B.   Sh command

C.   { command; }

D.   (( command ))

2: Using "awk", what would the output of this command string be? echo "1 2 3" | awk '{for (i=1; i<=NF; i++) s=s+$i};END {print s}'

A.   6

B.   123

C.   3

D.   600

3: The command below will search the root filesystem for files named "finance.db". In this context, what information is being sent to /dev/null? find / -name "finance.db" 1>results.txt 2>/dev/null

A.   The names of files that do not match finance.db

B.   Information sent to the standard error-for example, errors that the find command displays as it runs

C.   The names of files that match finance.db

D.   Information sent to the standard output-that is, the path to files the find command has located

4: To permanently remove empty lines from a file called textfile, which command could you use?

A.   Sed -i '/^$/d' textfile

B.   Sed '/^$/d' textfile

C.   Cat textfile | sed '/^$/d

D.   Sed -i 's/^$//' textfile

5: Assuming that user1 existed, what would be the result of this command string? awk -F: '/user1/{print $1 "-" $3 "-" $6}' /etc/passwd

A.   It would show the username, UID, and home directory of user1 separated by colons.

B.   It would print the UID, GID, and home directory of user1 separated by hyphens.

C.   It would print the UID, comment, and home directory of user1 separated by hyphens.

D.   It would show the username, UID, and home directory of user1 separated by hyphens.

6: What happens if you use the "set -e" in a Bash script?

A.   It will cause Bash to exit if a function or subshell returns a nonzero status code.

B.   It will cause Bash to exit if a conditional returns a non-zero status code.

C.   It will cause Bash to exit if local, declare, or typeset assignments return a nonzero status code.

D.   It will cause Bash to exit if a command, list of commands, compound command, or potentially a pipeline returns a nonzero status code.

7: The _ keyword pauses the script to get input from standard input.

A.   Get

B.   Argument

C.   Read

D.   Input

8: If file.sql holds SQL statements to be executed, what will be in file.txt? mysql < file.sql > file.txt

A.   A copy of the contents of file.sql

B.   An error indicating that this is invalid syntax

C.   The error output of the MySQL command

D.   The non-error output of the MySQL command

9: How does the SUID or setuid affect executable commands?

A.   When the command creates files, they will be owned by the group owner of the command.

B.   The SUID bit allows anyone to execute the command no matter what other permissions are set.

C.   When the command is executed, its running privileges elevate to the user owner of the command.

D.   When the command is executed, its running privileges elevate to the group owner of the command.

10: In order to extract text from the first column of file called textfile, which command would you use?

A.   Cat {$1,textfile}

B.   Cat textfile | awk [print $1]

C.   Cat textfile | awk '{print $1}'

D.   Awk textfile {print $1}

A.   Esc + R

B.   Ctrl + H

C.   Ctrl + R

D.   Alt + R

12: Which arithmetic expression will give the most precise answer?

A.   Var=$( expr 10 / 8 )

B.   (( var= 10 /8 ))

C.   Var=$(( 10 / 8 ))

D.   Var=$(echo 'scale=2; 10 / 8' | bc)

13: What is the result of this script? txt=Penguins [[ $txt =~ [a-z]{8} ]]; echo $?

A.   0, representing 'true', because the variable

B.   0, representing 'true', because everybody loves penguins!

C.   1, representing 'false', because the variable "txt" is longer than eight characters

D.   1, representing 'false', because the variable "txt" does not contain eight lowercase letters between a and z

14: How would you change your Bash shell prompt to the following? HAL>

A.   SHELL="HAL\>"

B.   SHELL="HAL>"

C.   export PS1="HAL>"

D.   PS1="HAL\>"

15: What is the output of this code? VAR="/var/www/html/website.com/html/"; echo "${VAR#*/html}"

A.   /website.com/html/

B.   /html/website.com/html/

C.   /var/www/html/website.com/

D.   Nothing will be echoed on the screen.

16: If prompted for text at the standard input, you can tell the command you're done entering text with what key combination?

A.   Ctrl + A (Windows) or Command + A (Mac)

B.   Ctrl + E (Windows) or Command + E (Mac)

C.   Ctrl + D (Windows) or Command + D (Mac)

D.   Ctrl + Z (Windows) or Command + Z (Mac)

17: In order for a Bash script to be executed like an OS command, it should start with a shebang line. What does this look like?

A.   #!/usr/bin/env bash

B.   ~/usr/bin/env bash

C.   '$!/usr/bin/env bash

D.   #/usr/bin/env bash

18: What line of Bash script probably produced the output shown below? The date is: Sun Mar 24 12:30:06 CST 2019!

A.   echo "The date is: !"

B.   echo "The date is: date!"

C.   echo "The date is: (date)!"

D.   echo "The date is: $(date)!"

19: Suppose your current working directory is your home directory. How could you run the script demo.sh that is located in your home directory? Find three correct answers. A. /home/demo.sh B. ./demo.sh C. ~/demo.sh D. bash /home/demo.sh E. bash demo.sh

A.   B, C, E

B.   A, B, C

C.   C, D, E

D.   B, D, E

20: How could you get a list of all .html files in your tree?

A.   Find . -type html

B.   Find . -name *.html

C.   Find *.html

D.   Find . -name \*.html -print

21: What would be in out.txt? cat < in.txt > out.txt

A.   The output from the command line. By default STDIN comes from the keyboard.

B.   Nothing because you can't redirect from file (in.txt) to another file (out.txt). You can only redirect from a command to a file.

C.   It would be the contents of in.txt.

D.   Nothing. The redirect will create a new empty file but there will not be any output from the cat command to redirect.

22: What does this bash statement do? (( $a == $b ); echo $?

A.   It loops between the values of $a and $b.

B.   It tests whether the values of variables $a and $b are equal.

C.   It returns $b if it is larger than $a.

D.   It returns $a if it is larger than $b.

23: What do you use in a case statement to tell Bash that you're done with a specific test?

A.   ; ;

B.   : :

C.   Done

D.   $$

24: What Bash script will correctly create these files?

A.   Touch file{1+10}.txt

B.   Touch file{1-10}.txt

C.   Touch file{1..10}.txt

D.   Touch file(1..10).txt

25: Which variable would you check to verify that the last command executed successfully?

A.   $$

B.   $?

C.   $!

D.   $@

26: How can you gather history together for multiple terminals?

A.   It just works by default.

B.   History --shared

C.   History --combined

D.   Shopt -s histappend

27: What is the difference between the $@ and $* variables?

A.   $@ treats each quoted argument as a separate entity. $* treats the entire argument string as one entity.

B.   $* treats each quoted argument as a separate entity. $@ treats the entire argument string as one entity.

C.   $* is used to count the arguments passed to a script, $@ provides all arguments in one string.

D.   $* is the wildcard that includes all arguments with word splitting, $@ holds the same data but in an array.

28: Which file allows you to save modifications to the shell environment across sessions?

A.   /etc/bash.conf

B.   ~/.profile

C.   /etc/bashprofile

D.   ~/profile

29: What file would match the code below? ls Hello[[.vertical-line.]]World

A.   Nothing, this is an invalid file glob.

B.   Hello.vertical-line.World

C.   Hello[[.vertical-line.]]World

D.   Hello|World

30: What will be in out.txt? ls nonexistentfile | grep "No such file" > out.txt

A.   No such file

B.   Ls: cannot access nonexistentfile: No such file or directory

C.   Nothing, out.txt will be empty.

D.   It will be the contents of nonexistentfile.

31: What will be the difference between the output on the screen and the contents of out.txt mysql < file.sql > out.txt

A.   The output on the screen will be identical to out.txt

B.   There will be no output on the screen as it's being redirected to out.txt.

C.   The output on the screen will be identical to out.txt plus line numbers.

D.   The out.txt file will hold STDERR and STDOUT will go to the screen.

32: How would you find the last copy command run in your history?

A.   History | find cp

B.   History | grep cp

C.   Grep cp history

D.   Cp history

33: In order to write a script that iterates through the files in a directory, which of the following could you use?

A.   Bash for i in $(ls); do ... done

B.   Bash for $(ls); do ... done

C.   Bash for i in $ls; do ... done

D.   Bash for $ls; do ... done

34: When executing a command and passing the output of that command to another command, which character allows you to chain these commands together?

A.   |

B.   ->

C.   #

D.   @

35: Which statement checks whether the variable num is greater than five?

A.   (( \$num -gt 5 ))

B.   [[$num -lt 5]]

C.   (( \$num > 5 ))

D.   \$num > 5

36: When used from within a script, which variable contains the name of the script?

A.   $0

B.   $# // number of positional parameters

C.   $$ // pid of the current shell

D.   $@ // array-like construct of all positional parameters

37: What does the + signify at the end of the 10-digit file permissions on data.txt? ls -l -rwx------+ 1 user1 u1 0 Oct 1 10:00 data.txt

A.   There is an SELinux security context

B.   The sticky bit is set and the file will stay in RAM for speed

C.   There is an access control list

D.   There is an extended attribute such as immutable set

38: In Bash, what does the comment below do? cd -

A.   It moves you to the directory you were previously in.

B.   It moves you to your home folder (whatever your current working directory happens to be).

C.   It deletes the current directory

D.   It moves you one directory above your current working directory.

39: What does this command do? cat > notes -

A.   Accepts text from standard input and places it in "notes"

B.   Creates "notes" and exits

C.   Outputs the content of notes and deletes it

D.   Appends text to the existing "notes"

40: What is the output of: VAR="This old man came rolling" echo "\${VAR//man/rolling}"

A.   This old rolling came rolling

B.   This old man came man

C.   This old man came rolling

D.   This old came

41: The shell looks at the contents of a particular variable to identify which programs it can run. What is the name of this variable?

A.   $INCLUDE

B.   $PATH

C.   $PROGRAM

D.   $PATHS

42: What does this command sequence do? cat >notes -

A.   It creates an empty file called

B.   It accepts text from the standard input and places it in the

C.   It appends text to an existing file called

D.   It outputs the contents of the

43: What is the output of this code? VAR="This old man came rolling" echo "${VAR//man/rolling}"

A.   This old man came man

B.   This old man came rolling

C.   This old rolling came rolling

D.   This old came

44: What statement would you use to print this in the console? Shall we play a game? yes\no

A.   echo "Shall we play a game? yes/\no"

B.   echo "Shall we play a game\? yes\\no"

C.   echo "Shall we play a game? yes\\no"

D.   echo "Shall we play a game? yes\no"

45: Which one is true?

A.   SELinux policy rules are checked after DAC rules.

B.   SELinux policy rules are checked before DAC rules

C.   SELinux policy rules are never checked after DAC rules.

D.   None of these

46: Which does the below command do? w

A.   It doesn't display information about the users currently on the machine.

B.   It displays information about the users currently on the machine.

C.   It displays information about the users currently on the another machine.

D.   None of these

47: Which sed options should you use to change the second-to-last instance of variable to rock so it would read: A constant is a variable that is a rock that isn't variable var="A constant is a variable that is a variable that isn't variable" echo "$var" | sed _____

A.   S/(.*)variable(.*variable)/\1rock\2/'

B.   S/variable/rock/'

C.   S/variable/rock/g'

D.   S/(.*)variable(.*variable)/\1rock\2/'

48: To make a Bash script named script.sh executable, what should you run?

A.   Exec script.sh

B.   Chmod +x script.sh

C.   Bash script.sh

D.   Source script.sh

49: How can you create a shared terminal in a Bash shell?

A.   Screen

B.   Screen -X

C.   Screen --shared

D.   Terminal -shared

50: Wich operator sends the output of ls to a file for later use?

A.   Ls < filelist.txt

B.   Ls ¦ filelist.txt

C.   Ls > filelist.txt

D.   Ls - filelist.txt