Just playing around with bash namerefs:
path_replace_file () {
local -n pathvar="$1"
pathvar="${pathvar%/*}/$2"
}
pathname="/home/user/tmp/data/sample1.txt"
printf 'pathname before = %s\n' "$pathname"
path_replace_file pathname extended_sample.dat
printf 'pathname after = %s\n' "$pathname"
Output:
pathname before = /home/user/tmp/data/sample1.txt
pathname after = /home/user/tmp/data/extended_sample.dat
The path_replace_file function takes a variable name as its first argument. In the function pathvar acts like an alias (a name reference) for that variable.
The function replaces the filename portion of the value of $pathval with whatever is given as the second argument to the function.
This requires bash 4.3 or later.
Changing local -n to typeset -n will make it work in ksh93 too.