LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 09-11-2007, 11:54 AM   #1
ocicat
Member
 
Registered: May 2007
Posts: 208

Rep: Reputation: 48
converting to tilde shorthand?


Given that the shell understands tilde (sigil) notation:
Code:
$ cd ~
$ pwd
/home/username
$
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):
Code:
#!/bin/sh
s=$(echo "$PWD" | sed "s!^$HOME\(.*$\)!~\1!")
...but is there some utility which already accomplishes the same functionality?

Thanks.

Last edited by ocicat; 09-11-2007 at 12:13 PM.
 
Old 09-11-2007, 07:32 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 03:08 AM   #3
gnashley
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 '~'.
 
  


Reply



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 05:33 PM
Tilde name shortening steveel Slackware 6 11-21-2005 03:55 AM
Shorthand or Autocomplete For Linux heratiki Linux - Software 3 03-14-2005 01:39 PM
tilde in X nva Linux - General 2 06-30-2003 11:12 AM
Filenames with tilde rovoreed Programming 2 08-08-2001 04:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:05 PM.

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