Relative path to the home directory (linux)

Viewed 693

When I use pwd it returns:

/home/ahmad/multifit-pretrain-lm/multifit/datasets

I look for a command so that I get the path relative to the home directory

multifit-pretrain-lm/multifit/datasets
2 Answers

I look for a command so that I get the path relative to the home directory

You can use bash parameter substitution here:

echo "${PWD#"$HOME"/}"
  • $PWD shell variable returns same value as the command pwd
  • ${PWD#"$HOME"/} strips "$HOME"/ at the start from $PWD

Variable substitution is a good solution but since you asked for a command, here is one:

realpath --relative-base="$HOME" .
Related