11

Today when writing my shell script.

A question suddenly comes to my mind.

Since cd /target_dir and cd /target_dir/ both works.
Should I add a slash at the end of my path variables in a shell script?
Such as LOG_PATH=/data/nginx/logs versus LOG_PATH=/data/nginx/logs/.

I did some gross search on google, but didn't find discussion about this, maybe it's too basic?

For now, it's really hard for me to decide which style to pick.
But I preferred LOG_PATH=/target_dir/ style a bit more.
Because when I'm doing autocompletion with bash, it pops me the result with slash.

What's your opinion about this, why?

closed as primarily opinion-based by andcoz, Stephen Kitt, Anthon, G-Man, X Tian Jun 29 '15 at 14:23

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

8

According to POSIX:

Definition of a pathname:

A string that is used to identify a file. It has optional beginning < slash > characters, followed by zero or more filenames separated by < slash > characters. A pathname can optionally contain one or more trailing < slash > characters. Multiple successive < slash > characters are considered to be the same as one < slash >, except for the case of exactly two leading < slash > characters.

  • @It's interesting, that I can cd to // and / with their name show differently on the bash prompt, and using pwd I'll get different path showed, but their content are identical! Why? – Zen Jun 29 '15 at 10:44
  • 1
    @Zen unix.stackexchange.com/a/12291/70524 – muru Jun 29 '15 at 10:44
  • 1
    Because bash tracks the current directory in a very naive way, as a string. It just appends and removes from the path heuristically, it does not link to the actual filesystem. One consequence is that you can cd into a symbolic link and come back the same way (if bash didn't decide it was too much and reinitialized it). The other is what you describe. You should not rely on the shell's tracking of the current directory, it is unreliable. – orion Jun 29 '15 at 10:46
  • Related: Strange difference between pwd and /bin/pwd. – G-Man Jun 29 '15 at 13:43
6

To be on the safe side, include the slash. This can lead to multiple slashes when concatenating the paths, but at least you avoid problems.

A few examples: rsync treats paths differently if the trailing slash is included (it synchronizes that directory instead of making another subdirectory). Symbolic links to directories sometimes behave in an unexpected way when they don't have the trailing slash - at least shell completion gets confused. You never know if the command/script you invoke relies on checking for the slash for some special behaviour. It could even save you from overwriting something. For instance, if you have a file named foo, but you mistakenly think it's a directory and want to move something in it, then mv bar foo will overwrite the file (data loss, potential catastrophe) but mv bar foo/ will just complain and do nothing.

So to conclude, it most cases it doesn't matter, but you should use the slash to protect yourself, and also to make it more obvious to the human reader what you intended to do in a script. A casual observer will immediately be sure that a variable refers to a directory if it ends with a slash, and will use it correctly if it needs to be modified.

2

No, you should not. It adds an extra unnecessary slash (/).

example

say you want to export java's bin directory to your PATH variable ,

export PATH=$PATH:/opt/jre1.7.0_45/bin/

now check it,

user@host:~$ which java
/opt/jre1.7.0_45/bin//java

notice the extra slash (/) before java, but fortunately it just works in such case.

  • I saw in this link there is a 31 votes answer in which the author thinks we should add a slash. I'm confused. stackoverflow.com/questions/980255/… – Zen Jun 29 '15 at 10:08
  • @Zen, yes I checked it, It was on the first comment of your question. Thanks. – Arnab Jun 29 '15 at 10:12
  • 4
    Better two slashes than none. This one is ugly but safe... the other option is way worse and can be dangerous. – orion Jun 29 '15 at 10:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.