0

I am wondering what is the difference between

$ ~ 

and

$ cd

Both seems to go to the same directory.

2
  • 10
    This is not default behaviour. You probably have an option set that allows you to cd to a directory just by giving its name. Commented Jun 29, 2017 at 15:21
  • 1
    Look for autocd in shopt's output and in man bash.
    – user62916
    Commented Jun 29, 2017 at 16:30

2 Answers 2

2

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.

1

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?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .