LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 05-26-2010, 09:31 PM   #1
youarefunny
Member
 
Registered: Mar 2010
Distribution: Arch Linux, FreeBSD
Posts: 31

Rep: Reputation: 0
Question Using tilde and spaces


I'm trying to use a tilde '~' for a filename. This works fine normally
Code:
rm ~/File
But when I have a file with a space in in and enclose it in quotes it takes it literally.

Code:
rm "~/Da File"
rm: cannot remove `~/Da File': No such file or directory
I have tried many things.

Code:
rm ~"/Da File"
rm '~/Da File'
rm $(~/Da File)
rm ~'/Da File'
rm "$(~/Da File)"
But no success. Any help is appreciated thank you.

Solution:
Quote:
Code:
rm ~/"Da File"
Reasons explained in the BASH reference guide (or man bash at your pleasure):
Quote:
If a word begins with an unquoted tilde character (~), all of the characters up to the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix.

Last edited by youarefunny; 05-27-2010 at 05:44 PM. Reason: Solved
 
Old 05-26-2010, 09:49 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,652

Rep: Reputation: 536Reputation: 536Reputation: 536Reputation: 536Reputation: 536Reputation: 536
try $HOME instead of ~
 
Old 05-27-2010, 02:31 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by youarefunny View Post
I'm trying to use a tilde '~' for a filename. This works fine normally
Code:
rm ~/File
But when I have a file with a space in in and enclose it in quotes it takes it literally.

Code:
rm "~/Da File"
rm: cannot remove `~/Da File': No such file or directory
I have tried many things.

Code:
rm ~"/Da File"
rm '~/Da File'
rm $(~/Da File)
rm ~'/Da File'
rm "$(~/Da File)"
But no success. Any help is appreciated thank you.
Here is a (probably an overkill) hint for you:

Code:
echo `echo ~`/'Da File'
.

Rather,

Code:
echo "'"`echo ~`/'Da File'"'"
.

Last edited by Sergei Steshenko; 05-27-2010 at 02:35 AM.
 
Old 05-27-2010, 02:44 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You missed
Code:
rm ~/"Da File"
Reasons explained in the BASH reference guide (or man bash at your pleasure):
Quote:
If a word begins with an unquoted tilde character (‘~’), all of the characters up to the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix.
 
Old 05-27-2010, 09:56 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Move to Linux-General (Not strictly a programming question)

It seems that ~ expands to $HOME, but only when not quoted?
 
Old 05-27-2010, 11:38 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Yes. If enclosed in double quotes ~ retains its literal meaning. If not quoted, bash performs tilde expansion and considers the tilde-prefix up to the first unquoted /. This means that in commands like
Code:
ls ~user/directory/*.png"
ls ~user/"dir with spaces"/*.ps"
ls ~user
~user is the tilde-prefix expanded to the user's home, whereas in
Code:
ls ~"user"
ls ~"/dir with spaces"
nothing is treated as a tilde-prefix and therefore there isn't any tilde expansion.
 
  


Reply

Tags
bash, file, filename, home directory, name, spaces


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File name followed by a tilde ~ malikah Linux - Newbie 2 02-17-2009 05:10 PM
tilde symbol for what purpose Karthikeyan Gurusamy Linux - Newbie 4 01-17-2006 05:22 AM
Tilde name shortening steveel Slackware 6 11-21-2005 04:55 AM
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


All times are GMT -5. The time now is 04:11 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration