I am wondering what is the difference between
$ ~
and
$ cd
Both seems to go to the same directory.
Nope, They both are different.
~
(tilde) : It represents the home directory of current active user, Eg:
Let's assume i have two user accounts i.e. root, sam.
when logged in as root the output of echo ~
will be /root
.
when logged in as sam the output of echo ~
will be /home/sam
.
cd
(change directory)
It is command used for changing the current directory where it takes path as argument and change directory according to that, Eg:
cd /home/sam/Desktop/
It will change current directory to the desktop of the user.
But when we type only cd it takes to home directory.
When no arguments are given it takes back you to the logined directory.
The ~
(tilde) is an alias for your home directory, and cd
is the command to change directories.
You can give cd
an argument like cd /var/log/
, and it will take you into that directory. If you don't give it an argument, it will take you into your home directory. ~
is another way to say "my home directory". That means that you can use commands like ~/myscript.sh
to do things inside that directory.
For more information on ~
, see this answer: Why was '~' chosen to represent the home directory?
cd
to a directory just by giving its name.autocd
inshopt
's output and inman bash
.