Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

is there any way (what is the easiest way in bash) to combine the following:

mkdir foo
cd foo

The manpage for mkdir does not describe anything like that, maybe there is a fancy version of mkdir? I know that cd has to be shell builtin, so the same would be true for the fancy mkdir...


share|improve this question

marked as duplicate by Stéphane Chazelas, Mikel, slm, Bernhard, jasonwryan Apr 18 '14 at 19:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

You can't alias two commands together directly. – goldilocks Apr 18 '14 at 14:05
Yes, seems to be a duplicate. Anything to do for me about that? – Jasper Apr 18 '14 at 16:21
Nope, just wait for it to be closed. Do not delete it, as being a different wording, it may still be useful for people searching the same based on terms in your question. – Stéphane Chazelas Apr 18 '14 at 16:52

4 Answers 4


mkcdir ()
    mkdir -p -- "$1" &&
      cd -P -- "$1"
share|improve this answer
quotes around "$1" will protect the argument if it has spaces. – glenn jackman Apr 18 '14 at 13:33
and put this in .bashrc? – Jasper Apr 18 '14 at 14:05
exactly ;) ... no big deal – Ouki Apr 18 '14 at 14:25
@glenn jackman: thanks for the "$1" – Ouki Apr 18 '14 at 14:26

I think creating a function is the most appropriate way to do this, but just for listing all alternative ways, you could write:

mkdir foo && cd $_

$_is a special parameter that holds the last argument of the previous command.

share|improve this answer
Neat, learned something new again! – Lekensteyn Apr 19 '14 at 13:31

These other lads are just making life complicated, here it is:

eval {mkdir,cd}\ FOLDER\;
share|improve this answer
This is really nice! – Radu Rădeanu Apr 18 '14 at 17:18
I really wanted to make a directory called 'FOLDER && rm ../something'. But eval won't let me :*( Tragically, may have to choose complicated in the battle of complicated vs evil. – BroSlow Apr 19 '14 at 2:14
@BroSlow, well then you'll have to find another OS, since having a slash in a filename has never been allowed! as for the other characters they work just fine if escaped. – Sean D Apr 19 '14 at 10:20
@SeanD You're missing my point. Using eval with user input is generally a bad idea and eval will process FOLDER && rm ../something in place of FOLDER without complaint. Or for another variant, how about "FOLDER && rm -r $HOME" – BroSlow Apr 19 '14 at 17:28
@BroSlow, using microwaves is generally a bad idea too, since you might put in tinfoil. – Sean D Apr 20 '14 at 9:30

Bash (using word designators):

/tmp/bug$ mkdir "some dir"
/tmp/bug$ cd !$
cd "some dir"
/tmp/bug/some dir$ 

!$ expands to the last argument of the previous line in the history. If you have parameters in between, then you can use !:1 for the first argument, !:2 forthe second argument, etc.

From bash(1):

Event Designators

An event designator is a reference to a command line entry in the history list. Unless the reference is absolute, events are relative to the current position in the history list.

! Start a history substitution, except when followed by a blank, newline, carriage return, = or ( (when the extglob shell option is enabled using the shopt builtin).


Word Designators

Word designators are used to select desired words from the event. A : separates the event specification from the word designator. [..]

n The n-th word.
^ The first argument. That is, word 1.
$ The last word. This is usually the last argument, but will expand to the zeroth word if there is only one word in the line.

share|improve this answer
I think that "!:1" loses vs "$firstfewcharactersofnewdir+TAB" in most cases... – Jasper Apr 18 '14 at 14:19
@Jasper I seldomly use !:1, but it is an option if you have a path with a non-unique prefix. – Lekensteyn Apr 18 '14 at 14:34
!$ is shorter and easier to type on many keyboards. See also <Alt-.> or <Alt-_> – Stéphane Chazelas Apr 18 '14 at 14:51
@StephaneChazelas Nice one, I'll update the answer to use !$ instead. – Lekensteyn Apr 18 '14 at 15:06
@StephaneChazelas What is the difference between Alt+. and Alt+_? – Bernhard Apr 18 '14 at 19:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.