Getting short path for ssh

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?

Answers


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


Need Your Help

Pass .NET Bitmap object to COM (DirectShow filter)

c# .net com directshow iunknown

I'm trying to create a source filter that makes a live video stream based on a sequence of pictures.

Dismiss Keyboard When Leaving FormSheet View?

objective-c ipad uinavigationcontroller keyboard

I have a UINavigationController which uses Form Sheet views and I have viewController 1 which pushes viewController2.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.