Another point to pay attention to: command substitutions are usually unsafe at the end of a line .
This is obviously pretty far-fetched, but if you're really worried about safe input processing, you should use "$PWD" . See for example:
$ my_dir=$'/tmp/trailing_newline\n' $ mkdir -p "$my_dir" $ cd "$my_dir" $ pwd /tmp/trailing_newline $ printf "%q\n" "$(pwd)" "$PWD" /tmp/trailing_newline $'/tmp/trailing_newline\n' $ cd "$(pwd)" sh: cd: /tmp/trailing_newline: No such file or directory $ cd "$PWD"
You can bypass command substitution, but this is by no means beautiful. You can add a trailing character and then delete it with the parameter extension:
$ pwd_guarded="$(pwd; printf '#')" $ pwd_fixed="${pwd_guarded%$'\n'#}" $ printf "%q\n" "$pwd_fixed" $'/tmp/trailing_newline\n' $ cd "$pwd_fixed"
This is especially ugly because then you also need to remove the new pwd that pwd adds, which is usually removed by command substitution. It becomes a complete mess if you don't resort to non-POSIX constructs like $'' , so basically just use "$PWD" if you care about these things. Of course, it makes perfect sense to simply not support trailing newlines in directory names.
Zaak van dongen
source share