Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include parameters in a bash alias? [duplicate]

Tags:

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?

like image 784
Andrey Fedorov Avatar asked Nov 30 '09 18:11

Andrey Fedorov


People also ask

How do you pass arguments to alias commands?

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.

Can a bash alias take arguments?

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.

What does $@ do in bash script?

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.


1 Answers

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" } 
like image 176
just somebody Avatar answered Oct 13 '22 22:10

just somebody