2

It is fairly well known that cd / in Bash/terminal takes you to your root directory, regardless of where you were before:

brian@brian-linux:~/redis-3.0.3$ cd /
brian@brian-linux:/$ pwd
/
brian@brian-linux:/$ ls
bin    dev         initrd.img.old  libx32      opt   sbin  usr
boot   etc         lib             lost+found  proc  srv   var
cdrom  home        lib32           media       root  sys   vmlinuz
data   initrd.img  lib64           mnt         run   tmp   vmlinuz.old
brian@brian-linux:/$ 

I stumbled upon an interesting discovery today:

In Bash, cd // takes you to directory //, which is the same as the root directory, but is still described as //:

brian@brian-linux:~/redis-3.0.3$ cd //
brian@brian-linux://$ ls
bin    dev         initrd.img.old  libx32      opt   sbin  usr
boot   etc         lib             lost+found  proc  srv   var
cdrom  home        lib32           media       root  sys   vmlinuz
data   initrd.img  lib64           mnt         run   tmp   vmlinuz.old
brian@brian-linux://$ pwd
//

What's going on here, and why is this the case?


It's also worth noting that cd /// or any number of / greater than 2 will take you to the root directory /. However, from the root directory, cd // takes you to //:

brian@brian-linux:/$ cd ///
brian@brian-linux:/$ cd //
brian@brian-linux://$ 
heartyporridge
  • 1,151
  • 8
  • 25
  • 2
    http://askubuntu.com/questions/23808/what-is-the-double-slash-directory – Paul Roub Jul 28 '15 at 15:04
  • 2
    http://stackoverflow.com/questions/16840916/what-is-path-how-is-it-different-from – Paul Roub Jul 28 '15 at 15:04
  • 2
    http://unix.stackexchange.com/questions/11964/what-do-double-slashes-mean-in-unix-path-is-cd-dir-subdir-valid – Paul Roub Jul 28 '15 at 15:05
  • Nothing to do with bash -- you'd have the same thing in every other language, ie. `os.chdir()` in Python. Not a bash question, shouldn't be tagged bash, etc. – Charles Duffy Jul 28 '15 at 15:11
  • I observed that while `//` points to `/root` and rest all `//....//` point out to `/`! Kinda dumbstruck... – Am_I_Helpful Jul 28 '15 at 15:11
  • 1
    @shekharsuman, what `//` does is implementation-defined, so your operating system can make it point anywhere without violating POSIX. That said, `/root` is an unusual choice; it's mostly just `/`. – Charles Duffy Jul 28 '15 at 15:12
  • Thanks @CharlesDuffy for clarifying. I am going through all the links mentioned here. Thanks again.,, – Am_I_Helpful Jul 28 '15 at 15:13
  • 1
    Thanks @PaulRoub, sorry for the duplicate. Google wasn't behaving with the //. – heartyporridge Jul 28 '15 at 15:18

1 Answers1

1

You can have any number of / in file paths. /this/file/path is completely equivalent to /this//file/////path.

I'm not sure if this is the reason, but it means that you are less likely to get errors when automatically generating paths. For example:

get_project_root() {
    echo "/the/project/root/"
}

get_project_data_subdirectory() {
    echo "data"
}

If I wanted to use these functions to cd to the project's data directory I could do this:

project_root=$(get_project_root)
data_directory=$(get_project_data_directory)

cd "${project_root}/${data_directory}"

Now the cd command actually ran:

cd /the/project/root//data

Because I added a / inbetween the 2 variables. I did this because I might not know if the function will return a trailing /, it doesn't need to and some functions might not. The fact that you can have any number of slashes means that you can add them in to be safe without worrying about breaking the command.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Tim B
  • 3,033
  • 1
  • 23
  • 28
  • 1
    Double leading slashes are technically different actually. – Etan Reisner Jul 28 '15 at 15:06
  • ...and I've seen a few filesystems that honor those differences (ie. AFS). – Charles Duffy Jul 28 '15 at 15:11
  • Ah, I wasn't aware of that. On my system cd // is the same as cd /. – Tim B Jul 28 '15 at 15:13
  • BTW, you can't legally return strings from shell functions; you can emit them on stdout, but that's different. – Charles Duffy Jul 28 '15 at 15:13
  • @CharlesDuffy Very true, I've changed the code. When I started writing the answer I wasn't aiming for shell code, then changed my mind half way through. – Tim B Jul 28 '15 at 15:14
  • Made a few further fixes re: shell syntax; hope you don't mind. (Using `${foo}` vs `$foo` is pure stylistic preference when not trailed by characters valid in variable names, but `$foo` vs `"$foo"` has real differences in correctness). – Charles Duffy Jul 28 '15 at 15:15
  • @CharlesDuffy Not at all! Thanks. I'm pretty sure my shell syntax is often questionable. – Tim B Jul 28 '15 at 15:17