LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Using tilde and spaces (http://www.linuxquestions.org/questions/linux-general-1/using-tilde-and-spaces-810331/)

youarefunny 05-26-2010 09:31 PM

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.


AlucardZero 05-26-2010 09:49 PM

try $HOME instead of ~

Sergei Steshenko 05-27-2010 02:31 AM

Quote:

Originally Posted by youarefunny (Post 3982372)
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'"'"
.

colucix 05-27-2010 02:44 AM

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.

pixellany 05-27-2010 09:56 AM

Move to Linux-General (Not strictly a programming question)

It seems that ~ expands to $HOME, but only when not quoted?

colucix 05-27-2010 11:38 AM

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.


All times are GMT -5. The time now is 02:37 PM.