LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-27-2016, 07:20 AM   #1
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 423

Rep: Reputation: 33
Why cd // goes to /


Accidentally i typed cd // and it went me to / dir and bash line changed to

[username@linux-pc //]$ <-- double slashes

Tried the same with cd /// , cd //// but then it went back to normal

[username@linux-pc /]$ <-- one slash

Is this happening to everyone ?
 
Old 12-27-2016, 07:46 AM   #2
bbuske
Member
 
Registered: Dec 2016
Location: Venezuela
Distribution: Manjaro
Posts: 109

Rep: Reputation: 15
Have tried it myself on openSUSE 42.2 and it does exactly the same. Running ls on it shows that it is the same as / though.

Interesting finding.
 
Old 12-27-2016, 09:14 AM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,786

Rep: Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216
In all cases, the directory you are actually in is "/". See the output from "/bin/pwd".

Your shell keeps track internally of the path to your CWD, and "//" is mostly equivalent to "/". This internal path can be significant when symbolic links are involved. Try running the following 4 commands:
Code:
ln -s /etc /tmp/myetc
cd /tmp/myetc
pwd
/bin/pwd
Now try "cd ..". Your shell will move up on its internal path and put you in "/tmp", while the actual parent of "/etc" is "/".

Contrast this with:
Code:
cd /tmp/myetc
cd -P ..
The "-P" tells cd to follow the physical path, and it will put you in "/".

Last edited by rknichols; 12-27-2016 at 12:43 PM. Reason: Clarify that the first set is 4 commands, not 3 commands and a response.
 
2 members found this post helpful.
Old 12-27-2016, 09:45 AM   #4
dlb101010
Member
 
Registered: Dec 2016
Posts: 61

Rep: Reputation: 18
Nice explanation. I've done that typo myself, ending up in '//' that was actually '/' and wondered what was going on. Thanks, now I know.

(BTW, if I'm following things right, shouldn't the fourth line in your first code example should be '/tmp/myetc'?)

Dave
 
Old 12-27-2016, 09:47 AM   #5
bbuske
Member
 
Registered: Dec 2016
Location: Venezuela
Distribution: Manjaro
Posts: 109

Rep: Reputation: 15
Yeah nicely explained indeed!
Rep for you.
 
Old 12-27-2016, 09:49 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,786

Rep: Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216Reputation: 2216
Quote:
Originally Posted by dlb101010 View Post
(BTW, if I'm following things right, shouldn't the fourth line in your first code example should be '/tmp/myetc'?)
No, "/bin/pwd" is a system command that will show your current working directory independent of what your shell has stored. That's in contrast with just "pwd", which is a shell built-in.
 
1 members found this post helpful.
Old 12-27-2016, 09:58 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,182

Rep: Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394Reputation: 7394
Code:
user@host:/tmp/bash-4.4$ cd //
user@host://$ echo $PWD
//
user@host://$ cd ///
user@host:/$ echo $PWD
/
it is implemented that way in bash, but I don't know why.
what is more interesting:
Code:
user@host:/$ cd //
user@host://$ zsh
user@host // % echo $PWD
//
user@host // % cd //
user@host / % echo $PWD
/
 
Old 12-27-2016, 11:09 AM   #8
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
// can be interpreted by a POSIX implementation in an implementation-defined manner.

See here for a couple of links to the relevant POSIX sections: https://askubuntu.com/questions/2745...-a-path/274517

Bash maintains the double forward slashes in case the particular implementation using Bash actually treats these as different from a single forward slash.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration