Trying to create:
alias mcd="mkdir $1; cd $1"
Getting:
$ mcd foo usage: mkdir [-pv] [-m mode] directory ... -bash: foo: command not found
What am I doing wrong?
You can replace $@ with $1 if you only want the first argument. This creates a temporary function f , which is passed the arguments. Alias arguments are only passed at the end. Note that f is called at the very end of the alias.
Bash users need to understand that alias cannot take arguments and parameters. But we can use functions to take arguments and parameters while using alias commands.
Symbol: $# The symbol $# is used to retrieve the length or the number of arguments passed via the command line. When the symbol $@ or simply $1, $2, etc., is used, we ask for command-line input and store their values in a variable.
An alias can only substitute the first word of a command with some arbitrary text. It can not use parameters.
You can instead use a shell function:
mcd() { test -e "$1" || mkdir "$1" cd "$1" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With