This is really something that is up to you to decide. What could influence that decision could be thing like whether it looks good or whether it's readable.
In terms of execution speed, it wouldn't make a whole lot of difference.
Possibilities to create three subdirectories under the preexisting directory
mkdir /path/to/dir/a \
( cd /path/to/dir && mkdir a b c )
(the subshell allows you to skip the
cd back to the original working directory)
With a shell that knows brace expansions (this is essentially the same as the
cd-less version above since the shell would expand the brace expansion before calling
mkdir utility will likely use the
mkdir() C library function. This function does not change working directories before creating the directory it's set out to create1.
It's different when you execute scripts that care about being executed in a particular working directory. If the script expects to find files using relative paths out of its current working directory, then obviously you will have to make sure the the working directory is correct before running the command, possibly with
( cd directory && thescript )
... unless the script does this itself.
1 Slight correction: On Linux, the command
mkdir -p a/b/c will call
mkdir("c"), while on OpenBSD it will just call
mkdir("a/b/c"). On Linux, creating an additional single directory
mkdir a/b/c/d will just call
So, on Linux,
mkdir -p will indeed internally "do a
cd" to each intermediate path while creating the directories (while
mkdir with no
-p does not do that).
-p flag to the
mkdir utility causes it to create any missing intermediate directory, and obviously the BSD and Linux developers do this ever so slightly differently.