Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-11-2007, 12:54 PM   #1
Registered: May 2007
Posts: 208

Rep: Reputation: 48
converting to tilde shorthand?

Given that the shell understands tilde (sigil) notation:
$ cd ~
$ pwd
Is there any utility which will condense an absolute pathname down to using a tilde if possible? ie: /home/username/dev would be reduced down to ~/dev?

I know I can do this through sed(1):
s=$(echo "$PWD" | sed "s!^$HOME\(.*$\)!~\1!")
...but is there some utility which already accomplishes the same functionality?


Last edited by ocicat; 09-11-2007 at 01:13 PM.
Old 09-11-2007, 08:32 PM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,146

Rep: Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672
I think you'll find that that 'translation' ie '~' to $HOME of the current user is specific to the cd cmd, not the shell as a whole.
OTOH, I could be wrong....
Old 09-12-2007, 04:08 AM   #3
Amigo developer
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
No, the shell, bash anyway, understands the tilde in most situations -depends on which bash builtin feature you are using and how you are quoting.
As to the OP's question, I don't know of any existing utility which does what you want. Usually when you write a script it's better to use $HOME instead of the tilde so you don't have to worry or wonder about compatibility with sh or ash and you can follow consistent rules regarding shell variable expansion.
It kinda sounds like you need to do some conversion of scripts to make them non-user-specific. In that case, I'd suggest you use a bit of code which checks the first part of the dir listing to see if it matches the home directory of the current user and translate that to '$HOME'(unexpanded) and not to '~'.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tilde? What's the key combination to get it ? DOTT.EVARISTI Linux - Software 9 12-24-2018 06:33 PM
Tilde name shortening steveel Slackware 6 11-21-2005 04:55 AM
Shorthand or Autocomplete For Linux heratiki Linux - Software 3 03-14-2005 02:39 PM
tilde in X nva Linux - General 2 06-30-2003 12:12 PM
Filenames with tilde rovoreed Programming 2 08-08-2001 05:53 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:23 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration