I'm writing a shell script to be used with ssh/git.
I want to get the present directory relative to home, if possible.
The goal is to get the shortest path to be used with ssh, that is the path in context of the user.
For example, if my working directory was /home/jon/folder/other
, and I was user jon
with $HOME
being /home/jon
, I would get (~/
)folder/other
from the command. Tilde-slash between the parentheses optional.
So then I could give my user the instruction to use ssh at jon@server:folder/other
instead of longer jon@server:/home/jon/folder/other
.
Is there are trivial way to do this (which I'm feeling there is) or do I need a script to do that?
You can use the following expression:
${PWD#$HOME/}
The bash
operation ${string#substring}
strips substring
from the front of $string
(if it matches).
This might help you with future questions about bash
: Recommended online resources for learning bash scripting