First 50 of 200 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.
Continue in the practice tool above to load more questions, 50 at a time.
Question 1Shells and Shell Scripting
Which character sequence at the very start of a shell script file causes the kernel to run the script with the interpreter named after it?
- $#
- #:
- !!
- #! Correct answer.
Explanation
The two-character sequence #! (shebang or hashbang) must be the first two bytes of the file and is followed by the path of the interpreter, for example #!/bin/bash. When the kernel sees it, it executes the named interpreter and passes the script path as an argument.
Question 2Shells and Shell Scripting
A script contains the line `readonly LIMIT=10`. What happens when a later line in the same script executes `LIMIT=20`?
- The variable becomes an environment variable exported to children
- The variable is silently set to 20
- The assignment fails with an error and the script continues if the error is not fatal Correct answer.
- The script restarts with the new value
Explanation
`readonly` marks a variable as unchangeable, so a later assignment produces an error message and a non-zero status. The script keeps running unless `set -e` or explicit error handling stops it.
Question 3Shells and Shell Scripting
Which command prints the value of the shell variable `HOME` only when it is already set and non-empty, producing nothing otherwise?
- echo ${HOME:?$HOME}
- echo ${HOME#$HOME}
- echo ${HOME:+$HOME} Correct answer.
- echo ${HOME=$HOME}
Explanation
The `${var:+word}` form expands to `word` only if the variable is set and non-null, and to nothing otherwise. The `=` form assigns a default, `:?` raises an error, and `#` removes a prefix pattern.
Question 4Shells and Shell Scripting
Inside a shell function, which construct refers to the arguments passed to the function rather than to the whole script?
- $$
- $@ Correct answer.
- $!
- $0
Explanation
Within a function, `$@` and `$*` reflect the function's own positional parameters, and `$1`, `$2` and so on refer to the function arguments. `$0` stays the script name and the PID variables are unaffected by the call.
Question 5Shells and Shell Scripting
What is the exit status of a command pipeline `cmd1 | cmd2` in bash when the `pipefail` option is not enabled?
- Always zero regardless of the commands
- The sum of both exit statuses
- The exit status of cmd1
- The exit status of cmd2 Correct answer.
Explanation
By default a pipeline returns the status of its last command, so only cmd2 determines `$?`. Enabling `set -o pipefail` changes this so the pipeline fails if any component fails.
Question 6Shells and Shell Scripting
Which test expression is true when the file named in variable `f` exists and is a regular file?
- [ -e "$f" ]
- [ -x "$f" ]
- [ -f "$f" ] Correct answer.
- [ -d "$f" ]
Explanation
The `-f` primary is true for an existing regular file, which excludes directories and device nodes. `-d` tests for directories, `-e` for any existing path, and `-x` for the execute permission.
Question 7Shells and Shell Scripting
A script begins with `set -u`. What is the effect when the script later references an unset variable `$COUNT`?
- The variable is exported automatically
- The variable expands to an empty string
- The variable is created and set to zero
- The shell prints an error for the unset variable and exits Correct answer.
Explanation
`set -u` (nounset) makes expansion of an unset parameter an error that terminates a non-interactive shell. This catches typos early, and `${COUNT:-0}` can be used to supply a default safely.
Question 8Shells and Shell Scripting
How does `command1 && command2` differ from `command1 ; command2`?
- command2 runs only if command1 succeeds in the first form Correct answer.
- The first form runs the commands in parallel
- Both commands always run in both forms
- command1 is run in the background in the first form
Explanation
The `&&` operator is a logical AND list, so the right-hand command executes only when the left-hand command returns status zero. The semicolon simply separates commands, so the second runs regardless of the first result.
Question 9Shells and Shell Scripting
Which construct reads a file line by line into variable `line` while preserving leading whitespace?
- for line in $(cat file); do echo "$line"; done
- for line in file; do echo "$line"; done
- while read line; do echo "$line"; done <<< file
- while IFS= read -r line; do echo "$line"; done < file Correct answer.
Explanation
Setting `IFS=` for the read command prevents trimming of leading and trailing whitespace, and `-r` stops backslash interpretation. The redirection `< file` feeds the file on standard input for the whole loop.
Question 10Shells and Shell Scripting
In a `case` statement, which pattern matches any single character in shell globbing?
- ? Correct answer.
- [...]
- *
- .
Explanation
In case patterns the `?` wildcard matches exactly one character, `*` matches any string, and a bracket expression matches one character from a set. A dot is an ordinary literal character.
Question 11Shells and Shell Scripting
What does the command `exec 3> /tmp/log` accomplish in a script?
- It opens file descriptor 3 for writing to /tmp/log Correct answer.
- It replaces the shell with the program named 3
- It duplicates standard output to descriptor 3 only
- It writes the number 3 into /tmp/log
Explanation
Redirections may name a descriptor explicitly, so `3>` opens descriptor 3 on the given file for writing without starting a new process. Later commands can write to it with `>&3`.
Question 12Shells and Shell Scripting
A script needs to send both standard output and standard error of `make` into the same file. Which form works in bash?
- make > build.log 2> build.log
- make &> build.log 2> build.log
- make 2>&1 > build.log
- make > build.log 2>&1 Correct answer.
Explanation
The order matters: `> build.log` first points descriptor 1 at the file, then `2>&1` makes descriptor 2 a copy of the current descriptor 1. The reverse order would leave standard error on the terminal.
Question 13Shells and Shell Scripting
Which line correctly assigns the standard output of `date +%Y` to variable `year`?
- year=$(date +%Y) Correct answer.
- year=$[date +%Y]
- year=(date +%Y)
- year=`date +%Y
Explanation
The `$(...)` form is the modern command substitution syntax and captures the command's standard output with trailing newlines removed. Backticks are the older equivalent, but they must be paired correctly.
Question 14Shells and Shell Scripting
Which environment file does bash read for an interactive login shell, in addition to /etc/profile?
- /etc/bashrc
- ~/.bash_profile Correct answer.
- ~/.profile only
- ~/.bashrc only
Explanation
A login bash shell reads /etc/profile and then the first of ~/.bash_profile, ~/.bash_login or ~/.profile that exists. Non-login interactive shells read ~/.bashrc instead.
Question 15Shells and Shell Scripting
What does the builtin `source ./lib.sh` do differently from `./lib.sh`?
- It runs the script with the sh interpreter regardless of the shebang
- It runs the file in a subshell and returns its output
- It runs the file in the current shell so variables and functions persist Correct answer.
- It compiles the script before running it
Explanation
`source` (or `.`) reads and executes the file in the current shell environment, so functions and variable assignments remain available afterwards. Executing `./lib.sh` starts a child shell whose changes disappear on exit.
Question 16Shells and Shell Scripting
Which command lists all exported environment variables along with their values?
- set
- env Correct answer.
- alias
- export -p --all
Explanation
`env` prints the environment it passes to child processes, which is exactly the set of exported variables. The `set` builtin prints all shell variables and functions, exported or not.
Question 17Shells and Shell Scripting
In the arithmetic expression `$(( x += 3 ))`, what is the value of x afterwards if it was 4 before?
- 7 Correct answer.
- 12
- 4
- 3
Explanation
The `+=` operator adds the right operand to the variable and stores the result, so 4 plus 3 gives 7. Arithmetic expansion also returns that value as the expansion result.
Question 18Shells and Shell Scripting
Which statement about `local` inside a shell function is accurate?
- It makes the variable read-only
- It exports the variable to all child processes
- It limits the variable's scope to the function and its children Correct answer.
- It stores the variable in a file for later scripts
Explanation
`local` declares a variable visible only within the function body and to commands it calls, and the previous value is restored when the function returns. It does not export the variable or make it constant.
Question 19Shells and Shell Scripting
What exit status does the shell store in `$?` after a command is terminated by signal 15?
- 0
- 143 Correct answer.
- 15
- 255
Explanation
When a command dies from a signal, the shell reports 128 plus the signal number, so signal 15 gives 143. A plain 15 would be an ordinary exit status chosen by the program itself.
Question 20Shells and Shell Scripting
Which command produces the line numbers of all lines in file.txt that contain the string `error`?
- grep -v error file.txt
- grep -l error file.txt
- grep -n error file.txt Correct answer.
- grep -c error file.txt
Explanation
The `-n` option prefixes each matching line with its line number in the input file. The `-c` option counts matches, `-l` lists filenames, and `-v` inverts the match.
Question 21Shells and Shell Scripting
A pipeline `cat data | sort | uniq -c | sort -rn | head -5` is run. What does the final output represent?
- The five least frequent distinct lines in data
- The five most frequent distinct lines in data Correct answer.
- The five longest lines in data
- The first five lines of data in original order
Explanation
`uniq -c` counts consecutive duplicates after sorting, and `sort -rn` orders those counts numerically in descending order. `head -5` then keeps the five highest counts, which are the most frequent lines.
Question 22Shells and Shell Scripting
Which sed command replaces every occurrence of `foo` with `bar` on each line of a file?
- sed 's/foo/bar/1' file
- sed 's/foo/bar/' file
- sed '/foo/bar/g' file
- sed 's/foo/bar/g' file Correct answer.
Explanation
The `g` flag on the substitute command repeats the replacement for all matches on a line rather than only the first. Without it, only the leftmost match per line is changed.
Question 23Shells and Shell Scripting
In awk, which builtin variable holds the number of fields in the current record?
- FS
- NF Correct answer.
- NR
- $0
Explanation
`NF` is the number of fields in the current record and can also be used as `$NF` to reference the last field. `NR` is the running record number and `FS` is the input field separator.
Question 24Shells and Shell Scripting
Which command prints only the usernames from /etc/passwd, one per line?
- awk -F: '{print $3}' /etc/passwd
- cut -d: -f7 /etc/passwd
- cut -d: -f1 /etc/passwd Correct answer.
- sort -t: -k1 /etc/passwd
Explanation
The username is the first colon-separated field, so `cut -d: -f1` selects it. Field 7 is the login shell and field 3 is the numeric user ID.
Question 25Shells and Shell Scripting
A script uses a trap with `trap 'rm -f "$tmp"' EXIT`. When does the cleanup command run?
- Only when the script exits with a non-zero status
- When the shell exits for any reason, including a normal end Correct answer.
- Only when the function containing the trap returns
- Only when the script receives SIGINT
Explanation
The pseudo-signal EXIT is delivered when the shell terminates, whether normally or after an error, so the handler runs in all those cases. Using EXIT is the portable way to guarantee temporary-file cleanup.
Question 26Shells and Shell Scripting
Which shebang line makes a script run under bash regardless of the user's default login shell?
- #!/bin/bash Correct answer.
- #!bin/bash
- #/bin/bash
- #!/bin/sh -bash
Explanation
The kernel only treats the first two bytes of an executable text file as a directive when they are the characters #! followed by the interpreter path. The interpreter path must then be an absolute path to an existing binary, so #!/bin/bash is the portable way to pin a script to bash.
Question 27Shells and Shell Scripting
A script contains `for n in 1 2 3; do echo $n; done`. How many lines does it print and in what order?
- Three lines: 1, then 2, then 3 Correct answer.
- A single line with the value 6
- Three lines: 3, then 2, then 1
- One line containing '1 2 3'
Explanation
The for loop assigns each word of the list to the variable in turn and runs the body once per word, so echo prints one line per iteration in list order. No arithmetic is performed and the words are never combined into a single string.
Question 28Shells and Shell Scripting
A function is defined as `greet() { echo "Hi $1"; }`. What does calling `greet world` print?
- Hi $1
- Hi
- Hi world Correct answer.
- greet world
Explanation
Inside a shell function, positional parameters such as $1 refer to the arguments given to that function call, so $1 expands to world. The function therefore prints the literal text Hi followed by the argument.
Question 29Shells and Shell Scripting
Which test correctly checks that variable `count` holds a value greater than 10 using the bash builtin conditional syntax?
- [[ $count > 10 ]]
- [[ $count -gt 10 ]] Correct answer.
- [ $count -eq 10 ]
- [[ $count -gt 10
Explanation
Inside [[ ]], the -gt operator performs a numeric comparison, so it evaluates the integer value of count against 10. This is the standard way to compare integers in a conditional expression.
Question 30Shells and Shell Scripting
A script runs `read -r -p "Name: " name < /dev/tty`. Why is input taken from /dev/tty rather than standard input?
- To read from the controlling terminal even when stdin is redirected from a file or pipe Correct answer.
- To make the variable name exported to child processes
- To strip the trailing newline from the value
- To force the read builtin to time out after one second
Explanation
Redirecting the read builtin from /dev/tty attaches it to the controlling terminal, so the prompt still reaches the user even if the script's standard input comes from a file or pipeline. This is a common pattern for interactive prompts inside otherwise non-interactive scripts.
Question 31Shells and Shell Scripting
What is the result of `x=5; y=$((x++)); echo "$x $y"`?
- 6 5 Correct answer.
- 6 6
- 5 6
- 5 5
Explanation
The post-increment operator returns the current value of x before incrementing it. So y receives 5, x becomes 6, and the echo prints 6 followed by 5.
Question 32Shells and Shell Scripting
A script contains `while read -r user; do echo "$user"; done < users.txt`. What happens to the variable user after the loop finishes?
- It becomes an exported variable
- It is reset to the empty string by read
- It is unset because the loop body ran in a subshell
- It keeps the value of the last line read Correct answer.
Explanation
Because the file is fed to the loop with an input redirection on the done keyword, the loop runs in the current shell rather than a subshell. Variables assigned inside therefore persist, and user retains the last line that was read.
Question 33Shells and Shell Scripting
A script must run a cleanup routine both when it exits normally and when it is interrupted by SIGINT. Which trap definition achieves that?
- trap cleanup EXIT &
- trap 'cleanup' INT KILL
- trap 'cleanup' EXIT
- trap 'cleanup' EXIT INT Correct answer.
Explanation
A single trap command can list several signals, so naming EXIT and INT installs the same handler for normal termination and for SIGINT. The EXIT pseudo-signal covers any exit of the shell, including one caused by a signal that has its own handler.
Question 34Shells and Shell Scripting
A script sets `IFS=:` and then runs `while read -r a b; do echo "$a-$b"; done < data`. Given the line `red:green:blue`, what is printed?
- red:green:blue-
- red-green:blue Correct answer.
- red-green-blue
- red:green-blue
Explanation
read splits the line into fields using the characters in IFS, and with only two variables the last one receives the remainder of the line unsplit. So a gets red and b gets green:blue, producing red-green:blue.
Question 35Interfaces and Desktops
Which X11 component is responsible for drawing the window decorations (title bar and border) around application windows?
- The X client library
- The window manager Correct answer.
- The display manager
- The X server itself
Explanation
A window manager is an X client that controls placement, stacking, and decoration of top-level windows; without one, applications appear without frames. The X server only provides primitives and does not draw title bars. Display managers handle graphical login, and client libraries just speak the protocol.
Question 36Interfaces and Desktops
On a system using systemd, which target corresponds to a graphical login with a display manager?
- multi-user.target
- rescue.target
- graphical.target Correct answer.
- default.target
Explanation
graphical.target pulls in the units needed for a graphical session, including the display manager, and is the standard target for desktop systems. multi-user.target is text-only with networking, and rescue.target is a single-user maintenance mode. default.target is only a symlink pointing to whichever target is configured.
Question 37Interfaces and Desktops
Which environment variable tells X clients which X display to connect to?
- DISPLAY Correct answer.
- XAUTHORITY
- TERM
- XTERM
Explanation
DISPLAY has the form host:display.screen and is read by X clients to locate the server, for example :0 for the local first display. XAUTHORITY names the cookie file used for access control, not the server address. TERM describes the terminal type for text programs.
Question 38Interfaces and Desktops
Which file normally stores the per-user authorization cookie used by X clients to connect to the local X server?
- /etc/X11/Xresources
- ~/.xsession-errors
- ~/.Xauthority Correct answer.
- /etc/X11/xorg.conf
Explanation
The ~/.Xauthority file contains MIT-MAGIC-COOKIE entries that authorize the user to connect to an X display; tools like xauth manage it. xorg.conf configures the X server hardware, Xresources holds X resource defaults, and .xsession-errors captures session log output.
Question 39Interfaces and Desktops
A user wants to start the X server on virtual terminal 8 with the default display number. Which command accomplishes this?
- Xephyr :8
- startx -- :1 vt08
- xinit vt8 :0
- Xorg :0 vt8 Correct answer.
Explanation
The Xorg binary accepts a display argument and the vt option to choose the virtual terminal, so 'Xorg :0 vt8' starts display :0 on VT 8. startx wraps xinit and passes server arguments after a double dash, but the given form selects display :1, not :0. Xephyr is a nested server, not a VT-based one.
Question 40Interfaces and Desktops
Which command displays the currently running X server's version and command-line options?
- xrandr
- xdpyinfo Correct answer.
- xhost
- xwininfo
Explanation
xdpyinfo queries the X server and prints capabilities including the server version, vendor string, and screen details. xwininfo reports attributes of a specific window, xrandr manages screen resolution and rotation, and xhost controls host-based access.
Question 41Interfaces and Desktops
Which command grants another host named client1 access to your local X display using host-based authorization?
- xdpyinfo client1
- xhost +client1 Correct answer.
- xauth merge client1
- xauth add client1
Explanation
xhost manages the host-based access control list, and 'xhost +client1' adds that host to the list of permitted clients. xauth manipulates cookie entries but does not take a bare hostname that way. xdpyinfo only queries server information.
Question 42Interfaces and Desktops
What is the primary purpose of a display manager such as LightDM, GDM, or SDDM?
- To provide a graphical login screen and start the user's session Correct answer.
- To manage printing from graphical applications
- To composite windows and provide drop shadows
- To configure the X server's input devices
Explanation
A display manager authenticates users graphically and then launches the chosen desktop session, which is why it is often called a greeter. Compositing is done by a compositor or the window manager, input configuration belongs to the X server or udev, and printing uses CUPS.
Question 43Interfaces and Desktops
Which file, if present in a user's home directory, is executed by startx to define the programs run in the X session?
- ~/.bash_profile
- ~/.xinitrc Correct answer.
- ~/.dmrc
- ~/.Xresources
Explanation
startx runs the commands in ~/.xinitrc, which typically starts a window manager and other clients before the session ends. .bash_profile configures login shells, .Xresources holds resource defaults, and .dmrc records the last session chosen in a display manager.
Question 44Interfaces and Desktops
A remote user connects with ssh -X and starts a graphical program, but it fails with 'cannot open display'. Which setting most directly enables X11 forwarding for that connection?
- PermitRootLogin no in sshd_config
- X11Forwarding yes in sshd_config Correct answer.
- ForwardAgent yes in ssh_config
- UsePAM no in sshd_config
Explanation
The SSH server must have X11Forwarding enabled so it can set up a proxy display and cookie for the client. ForwardAgent controls SSH agent forwarding, which is unrelated to X. PermitRootLogin and UsePAM affect authentication and do not enable display forwarding.
Question 45Interfaces and Desktops
Which command lists the X resource database entries currently defined for the display?
- xset q
- xrdb -query Correct answer.
- xmodmap -pm
- xprop -root
Explanation
xrdb loads and queries the X resource database, and -query prints the current entries. xmodmap changes keymaps and modifiers, xset q shows server settings like the keyboard repeat rate, and xprop reads window properties.
Question 46Interfaces and Desktops
Which command changes the keyboard map so that the Caps Lock key acts as an additional Control key?
- xkbcomp -xkb $DISPLAY -
- setxkbmap -option ctrl:nocaps Correct answer.
- xmodmap -e 'clear Lock'
- xset r off
Explanation
setxkbmap applies an XKB option, and ctrl:nocaps maps Caps Lock to Control while disabling the lock behavior. Clearing only the Lock modifier with xmodmap does not by itself make Caps Lock generate Control. xkbcomp compiles keymaps, and xset r controls key repeat.
Question 47Interfaces and Desktops
Which X client is commonly used to take a screenshot of the whole root window and save it as a PNG file?
- xev -root
- xwd -root | convert shot.png
- scrot shot.png Correct answer.
- import -window root shot.png
Explanation
scrot is a dedicated command-line screenshot utility whose default action captures the entire screen to the named file, with the format inferred from the extension. xwd alone produces an X window dump that needs conversion, the import example omits the output filename, and xev reports events.
Question 48Interfaces and Desktops
Which Wayland compositor is a reference implementation that also provides a kiosk shell for single-application use?
- Weston Correct answer.
- KWin
- Sway
- Mutter
Explanation
Weston is the reference Wayland compositor maintained by the Wayland project and ships several shells, including the kiosk shell for running one application full screen. Mutter and KWin are GNOME and KDE compositors respectively, and Sway is an i3-compatible tiling compositor.
Question 49Interfaces and Desktops
Which environment variable selects the Wayland display socket a client should connect to?
- XDG_SESSION_TYPE
- WAYLAND_DISPLAY Correct answer.
- DISPLAY
- XDG_RUNTIME_DIR
Explanation
WAYLAND_DISPLAY names the socket, usually wayland-0, located inside the directory given by XDG_RUNTIME_DIR. XDG_SESSION_TYPE merely reports whether the session is wayland or x11, DISPLAY is for X11, and XDG_RUNTIME_DIR gives the directory but not the socket name.
Question 50Interfaces and Desktops
Which protocol does XWayland implement to allow legacy applications to run under a Wayland session?
- It runs an X server that presents X11 windows inside the Wayland compositor Correct answer.
- It tunnels X11 over SSH to a separate display server
- It translates Wayland clients into X11 clients on a remote host
- It replaces the Wayland compositor with an X11 window manager
Explanation
XWayland is an X server that runs as a Wayland client, so unmodified X11 applications draw into windows the compositor displays alongside native Wayland clients. It does not convert native Wayland clients or replace the compositor, and it is unrelated to SSH tunneling.