Pass pwd output to basename and store only the current directory name in CURRENT_DIR.
Use a syntax with clear nested delimiters.
Which assignment is appropriate?
$() has clear matching delimiters and supports nesting. Quote the inner substitution when passing its result as an argument.
Detailed explanation
CURRENT_DIR='basename pwd'Incorrect. It assigns literal text and executes neither command.
Incorrect. It assigns literal text and executes neither command.
CURRENT_DIR=$(pwd basename)Incorrect. It passes basename as an argument to pwd in the wrong order.
Incorrect. It passes basename as an argument to pwd in the wrong order.
CURRENT_DIR=${basename ${pwd}}Incorrect. This is not a valid nested parameter-expansion form.
Incorrect. This is not a valid nested parameter-expansion form.
CURRENT_DIR=$(basename "$(pwd)")Correct. The inner pwd result is passed to the outer basename command.
Correct. The inner pwd result is passed to the outer basename command.
Try it yourself
An example you can run in a temporary verification environment.
bash --noprofile --norc -c 'cd /tmp; CURRENT_DIR=$(basename "$(pwd)"); printf "%s\n" "$CURRENT_DIR"'Expected result
tmpKey points
- $() can be nested
- The inner substitution is evaluated first
- Quote substitution results when needed
Notes
- Environment: GNU Bash 5.2 / /tmpへ移動する一時シェル
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.