A script needs to create a temporary file in the directory from which it is executed. The script will be called by users from various locations, so hardcoding an absolute path is not feasible. To ensure the script can reliably determine its current working directory, which of the following lines would correctly assign the absolute path of the current directory to the EXEC_DIR variable?
The pwd command (print working directory) returns the absolute path of the current directory. Capturing its output with command substitution $(pwd) is the standard and most reliable method for this task. The distractor dirname $0 would return the directory containing the script file itself, which is not necessarily the same as the directory it was executed from. which $0 is not a valid use of which, as which locates a command in the user's PATH, it does not operate on script paths this way. The correct shell variable for the current working directory is PWD, not CWD, making EXEC_DIR=$CWD incorrect.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What does `pwd` stand for and why is it used?
Open an interactive chat with Bash
What is the difference between an absolute path and a relative path?