0

Am writing script using bash.

I have a name for a log file log and want to prepend a directory path $destin. But $destin can either end with / or without /.

log="$destin/$log"
0

1 Answer 1

2

There's variable expansions that do exactly that:

log="${destin%/}/$log"

The % removes the suffix that follows from the variable, if present.

However, this isn't ncecessary: `path//file" and "path/file" are the same file.

As Quasimodo points out:

If removing any number of slashes is really needed, log="$(dirname "$destin")/$(basename "$destin")" should do. This will also work if destin is something like a/b/c///.

1
  • You're right about path//file and path/file. Just be careful about removing all double slashes though, as POSIX allows for //path/file and /path/file to be different (a leading double slash is defined to be implementation dependent)
    – roaima
    Jul 19, 2021 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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