Make it a function instead:
crdir() {
if [ "$#" -eq 1 ]; then
mkdir -p -- "$1" && cd -P -- "$1"
else
echo >&2 "Usage: crdir <directory-path>"
return 1
fi
}
Which you can put in your ~/.bashrc
.
Your . crdir
approach would also work (in bash
and other shells whose .
can take arguments), but you'd need to remove that exit 0
which causes the shell to exit. Also note that when not in POSIX mode, bash
looks for the file to source in the current directory if it can't be found in $PATH
, which makes it a bit dangerous (at the same level as adding .
or an empty entry at the end of $PATH
).
Also if . crdir
is called without arguments, then the script will inherit the positional parameter of the shell. For instance, set foo; . crdir
would not return an error and would create the foo
directory.
A few other notes on your script:
#!/bin/bash
Note that the she-bang is ignored (is just seen as a comment) when the file is sourced.
if [[ $# -ne 1 ]]; then
echo "only use one argument"
It's preferable to output error messages on stderr (echo >&2 text
).
exit 1
fi
mkdir $1
Leaving parameter expansions unquoted has a special meaning in bash
, you should almost never do that.
Also, if $1
starts with a -
, it will be treated as an option by mkdir
. You'd need to use the --
end-of-option marker to tell mkdir
the content of $1
is not an option even if it starts with -
.
You're also not checking if the mkdir
command succeeded.
You may also want to use the -p
option which tells mkdir
to create all the directory components and also causes mkdir
not to return failure if the directory already existed. So:
mkdir -p -- "$1" &&
sleep 0.5
No need to sleep. You can be sure that the directory has been created when mkdir
returns successfully.
cd $1
Again, you've got the quoting and -
issues. Also, in bash
you need the -P
option for cd
to interpret paths the same way as other utilities like mkdir
. So:
cd -P -- "$1"
(ignoring for now a path called -
or $CDPATH
which I'd hope you don't use or use only with sane values).
exit 0
Again, you're not checking the exit status of cd
and returning with a success exit status, even if mkdir
or cd
failed. When a script is sourced, it's interpreted by the sourcing shell, so that exit
would cause it to exit.
Here, I'd rewrite the script as:
if [ "$#" -eq 1 ]; then
mkdir -p -- "$1" && cd -P -- "$1"
else
! echo >&2 "Usage: crdir <directory-path>"
fi
mkdir -p -- "$1"
? Also the lack of exit status checks is because I just wanted to see how to make it work first. – iamAguest Aug 28 '18 at 14:46