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.
CURRENT_DIR=$(pwd basename)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.
CURRENT_DIR=$(basename "$(pwd)")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
Nested substitution
A substitution placed inside another so the inner result becomes outer input.
basename
A command that extracts the final component of a path.
Double quotes
Quoting that permits expansion while preventing expanded whitespace from splitting words.