Which TWO statements about bash shell functions are correct? (Choose two.)
LPIC-1 Exam 102-500, objective 105. Shells and shell scripting medium
Machine-checked — no person has signed for it. This question was read against the source cited below by an automated pass, which found no contradiction. That is a weaker claim than it sounds: the same kind of process wrote the question, so it can confirm its own mistake.
Treat it as a good draft rather than as settled fact, and read the source below before you rely on it. It is not used in mock exams here — only questions a person has signed for are.
How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.
The options
Choose 2.
Correct A function refers to the arguments it was called with as $1, $2 and so on, and may use them in any position
Correct. A function call sets the positional parameters for the duration of the call, which is what lets a function place its arguments anywhere in the commands it runs.
Correct A function can be placed in the environment of child bash processes with export -f
Correct. export -f name marks a function for export, and a child bash inherits the definition. Aliases have no equivalent.
Not correct A function must be defined in ~/.bash_profile; definitions in ~/.bashrc are ignored
Wrong. ~/.bashrc is the usual home for functions, since it is read by every interactive non-login shell. Neither file is privileged for function definitions.
Not correct Defining a function requires the function keyword; the name() form is not valid in bash
Wrong. Both name() { ...; } and function name { ...; } are accepted, and only the first is POSIX-portable.
Not correct A function is removed from the current shell with unalias
Wrong. unalias removes aliases. A function is removed with unset -f name; plain unset without -f looks for a variable of that name first.
Why
A function is a named compound command stored in the shell, so it takes positional parameters, contains control flow and can be exported with export -f. An alias is a text substitution of the first word of a command, so it cannot see its arguments and is not inherited by child processes at all. Where the two overlap, prefer the function: it behaves the same in scripts, whereas alias expansion is off by default in non-interactive shells.
Where this comes from
- Cited
- manual page bash(1)
Practise this
Reading one question is not practice. The trainer will draw a short set from objective 105 and space the ones you get wrong.
More questions on this objective
- A user logs in at a text console, and bash starts as an interactive login shell. Which file in that user's home directory is read by bash itself as part of login startup? machine-checked
- You are already logged in to a graphical desktop and open a new terminal window, so bash starts as an interactive shell that is not a login shell. Which per-user file does bash read in that case? machine-checked
- An executable script contains only the line `export EDITOR=vim`. After running it as `./setenv.sh`, `echo $EDITOR` in the calling shell prints nothing. What explains this? machine-checked
- In bash, what is the effect of `export MYVAR`? machine-checked
- At a bash prompt you run `alias ll='ls -l'` and it works for the rest of that session, but a terminal window opened afterwards does not recognise `ll`. Which statement explains this and gives the standard remedy? machine-checked
- A command finishes and the shell reports an exit status of 0. What does that mean, and which parameter reports it? machine-checked