-1

under Ubuntu 18 Linux I try to set an alias in .bash_aliases with 2 commands in it, and reading an input parameter

alias nd="mkdir $1 && cd $1"

I do "nd qq", and the result is

mkdir: missing operand

Where is my error ? Any help is welcome

PD.- I have an alias with "parameter" that works fine :

alias mira="ps -ef | grep -v grep | grep $1"

I do "mira dhcp" and result is OK

PD2.- if I code

alias nd="mkdir $1" 

... it works perfect

LetsTalk
  • 3
  • 5

1 Answers1

1

Have you tried running alias nd afterward? The output is

alias nd='mkdir  && cd '

which should let you see that something has gone wrong.

When you feed the shell with

alias nd="mkdir $1 && cd $1"

the processes this line; among other things, the processing results in expanding the variable $1, which is empty in your current environment, as you can see by entering echo $1 in the terminal.

Bottom line: alias is not the tool for this. A function is the tool for this. Run the following in your shell, and then nd will do what you want.

nd() { mkdir $1 && cd $1; }

When you do

alias nd="mkdir $1" 

this explands to

alias nd='mkdir '

(the trailing space really does nothing) so you have just created another name for mkdir, much like you had done

alias nd=mkdir

and this is why it "works".

Enlico
  • 23,259
  • 6
  • 48
  • 102