What does the symbol '~' mean in Bash?

5

What does this symbol mean ?: ~

I've seen it appear when you are going to install some components such as:

php ~/composer.phar require cboden/ratchet

or when I make a cd in the root:

analistasistemas-VirtualBox / # cd 
analistasistemas-VirtualBox ~ # 

What name does the symbol have?

    
asked by Brayan Rodriguez 06.02.2018 в 17:09
source

6 answers

8

~ is a symbol called virgulilla that in UNIX operating systems refers to the value of the variable $HOME , that is, the directory of the user that is logged in.

This allows you to reference that directory directly, without having to type /home/usuario/ each time.

If you look at the file /etc/passwd , there you will see how it is defined for each of the users. Normally it is /home/nombre_de_usuario , although for root it is usually / :

Well, when Bash (or another shell, then is something defined by POSIX ) reads ~ outside of quotation marks, it expands it automatically by the value that is in /etc/passwd for the user that is executing it.

Therefore, in your particular case, by saying:

php ~/composer.phar require cboden/ratchet
#   ^

What will be done is:

php /home/tu_usuario/composer.phar require cboden/ratchet
#   ^^^^^^^^^^^^^^^^
    
answered by 06.02.2018 / 17:51
source
8

The '~' is the Absolute Path (path) of the user you are using, that is, the personal folder of the user with whom you are logged in.

For example, I logged in with the user: foo if I put

cd ~

is the same as putting

cd /home/foo

when doing a 'cd' in the root what it does is automatically add 'cd.' if you do an 'ls -a' you will see that there is a (symbolic) directory that puts './' that points to the directory where you are.

cd 

is the same as putting

cd ./

And the other is the behavior of PS1 (prompt), if you want to know more about PS1 I leave the link here .

    
answered by 06.02.2018 в 17:36
6

In Bash, the ~ or "tilde" (in English) is a special character whose most common use is to briefly indicate the special $HOME folder that corresponds to the user's personal directory. When we do something like this: cd ~ if we are the user pedro we will surely go to the folder /home/pedro . This use is so common that it is lost sight of that the ~ is a characteristic of Bash to expand the name of a folder with multiple uses well documented in Tilde Expansion .

Some uses:

  • ~ - > the value of $HOME
  • ~/foo - > $HOME/foo
  • ~fred/foo - > Subfolder foo in $HOME of user fred .
  • ~+ - > $PWD The current directory

Bash has a stack where the folders where we have been browsing are registered, we can use ~ to navigate this stack (see command dirs ):

  • ~ - - > $OLDPWD The previous directory where we have been
  • ~ 1 - > the same as the 'dirs +1' command (the later directory on the stack)
  • ~ -1 - > the same as the 'dirs -1' command (the previous directory on the stack)
answered by 06.02.2018 в 17:52
3

It is the path to your user's personal folder (which is usually accessible through the $ HOME environment variable).

For example, if your user is "pepe", your personal folder will be in / home / pepe and when you enter cd ~ you will go to that folder.

As a point to keep in mind, the superuser folder (root) is not in / home / root, but directly in the root of your system, that is, in / root.

    
answered by 09.03.2018 в 11:24
2

It is the relative path to the user's personal folder.

For example:

  

Absolute path: /home/usuario/imagen.jpg

     

Relative path: ~/imagen.jpg

    
answered by 06.02.2018 в 17:25
2

means the path of the user's home folder ~

If we have in our console something like:

miuser@server:~$

if you execute pwd it will return the path /home/miuser since it is placing the user's route

An example is if you are in a x folder and you want to return it to the user's folder, you can run it with the command:

miuser@server:/var/www/$ cd ~

this applies to all users, it will look for the user's folder with which you are working with some differences in root if you do not have that folder created

It may be that the user root is a bit different, it will return you to the path /root

Now if you have doubts exactly which route you are going to go to, execute the following command

echo ~

will return the route you are aiming

    
answered by 06.02.2018 в 18:02