3

When programming for Unix/Linux, is it best practice to use a trailing slash, or to omit it?

For example:

Trailing slash

baseDir="/path/to/dir/"

#code#

outPath=${baseDir}addition/to/path/

No Trailing Slash

baseDir="/path/to/dir"

#code#

outPath=${baseDir}/addition/to/path

#or:
outPath=$baseDir/addition/to/path

I know that technically using the trailing slash explicitly indicates the path refers to a directory, but I also know that in practice it rarely matters (and is often omitted when working at the command line). I also like that omitting the trailing slash makes it more explicit when I am appending to the path in a script.

Despite all this, I want to know if there is an explicit convention most people would expect my code to follow, or if this is a matter of personal taste.

Cole
  • 130
  • 5
  • 4
    I've never seen an explicit convention, and some applications (rsync) to different things with and without a trailing slash. In shell scripts I find it convenient to write $baseDir/addition instead of ${baseDir}addition, but that's just my personal preference. Also notes double slashes in paths path/todir//more/path/items are often interpreted correctly. – dirkt May 24 '19 at 08:28
  • Note that double slashes at the root of a path can be interpreted in an implementation dependent manner. – Chris Davies May 24 '19 at 09:59
  • This is down to the individual programmer's preferences (or to the preferences of their team). I closed this as a dupe of the question that muru suggested rather as "opinion-based" as referring to the dupe would be more constructive. – Kusalananda Jun 19 '19 at 07:16

0 Answers0