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.
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 pathspath/todir//more/path/items
are often interpreted correctly. – dirkt May 24 '19 at 08:28