Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have two instances of it being used and I am wondering what each does:

  1. service=~

  2. mv ~/Desktop/Service$version.tgz $service

What does the little squiggly ~ do?

Then, after that that, what would cd $service do?

share|improve this question

migrated from stackoverflow.com Apr 6 '11 at 0:37

This question came from our site for professional and enthusiast programmers.

2  
this so deserves a tag [squiggly] –  Cawas Apr 6 '11 at 7:25
3  
Tilde, is shell specific and not specific for linux. –  David Allan Finch Apr 6 '11 at 8:40
    
@Cawas: Ask, and ye shall receive. –  Dave Sherohman Apr 6 '11 at 9:17
2  
+1 for squiggly –  Phoshi Apr 6 '11 at 9:21
    
@Dave thanks! But seems like Chris hated it. Oh well, life goes on... –  Cawas Apr 6 '11 at 11:44

5 Answers 5

up vote 43 down vote accepted

The squiggly thing is called a "tilde".

It expands to your home directory.

Try

echo ~
echo $HOME

Both statements put your home directory by itself on a line..

See bash Tilde Expansion for details.

share|improve this answer
8  
The comparison above, while valid, is perhaps misleading: the tilde only works during shell interpretation in scripts and at the command line. The environment variable, $HOME, however, works anywhere an environment variable works - which is a lot more places. –  Mei Apr 6 '11 at 14:44

The "squiggly" is called a tilde. It is used to refer to your home directory which on Linux, is normally /home/username. It is also stored in the $HOME environment variable. Expanding the ~ to the location of the home directory is the job of the shell (like zsh or bash) or file manager (like Nautilus) and not the filesystem or OS its self.

You can also use this to refer to another user's home directory. For instance, if the other user's username is bob, you could refer to their home directory with ~bob, which will be expanded to /home/bob/.

The first example you've given sets the variable service to ~, so it corresponds to your home directory. This is equivalent to service=/home/username or service=$HOME.

The second example copies the file ~/Desktop/Service$version.tgz (or /home/username/Desktop/Service$version.tgz) to /home/username. This command is equivalent to:

mv ~/Desktop/Service$version.tgz ~

or

mv ~/Desktop/Service$version.tgz $HOME

or

mv ~/Desktop/Service$version.tgz /home/username/

The third will change the current working directory ($PWD) to /home/username/. This is equivalent to:

cd /home/username/

or

cd $HOME
share|improve this answer
3  
One additional thing to keep in mind: Tilde expansion is the job of the shell or the filemanager, it is not a function of the Linux filesystem itself. Thus it often won't work in configuration files and adding quotes around "~" it will stop its expansion in the shell. –  Grumbel Apr 6 '11 at 8:58
3  
It should also be noted that home directories don't necessarily lie in /home so it shouldn't be assumed that ~ expands to /home/[my user name] or that ~bob expands to /home/bob –  darkliquid Apr 6 '11 at 9:13
    
Thanks for the suggestion @Grumbel. And Thanks @PriceChild for adding in @darkliquid's suggestion! –  Wuffers Apr 6 '11 at 12:24
    
+1 for the ~bob which I had no idea you could do. –  Marlon Apr 7 '11 at 4:03

In both #1 & #2: ~ is your home directory, so if you are qwerty it will likely be the directory /home/qwerty. So try ls ~ to see that.

For #1: it looks to me like the variable service is being defined as your home directory.

That means after #2 has moved the tgz file from the Desktop subdirectory to your home directory, #3 then changes to the home directory.

share|improve this answer

It looks like the commands are doing the following.

  1. Assign a variable called service to your home folder location, for example:

    /home/user
    
  2. It moves the file from your desktop to the top level of your home directory, for example:

    /home/user/Desktop/Service$version.tgz $service
    
  3. The script then changes the directory to the top level of the home directory.

So, all the script is doing is just cleaning up your desktop by moving the file to your /home/user folder instead.

share|improve this answer

I'll add here that ~user also expands to [user]'s home directory, so it's not just a shortcut for your own home directory.

E.g.:

[guy@abox anotherdir]$ cd ~ 
[guy@abox ~]$ pwd
/home/guy
[guy@abox ~]$ cd ~john
[guy@abox john]$ pwd
/home/john
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.