Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does 'cd $_' mean?

Tags:

bash

I have seen this command in a tutorial to create a new directory:

mkdir my-new-project && cd $_ 

I know mkdir my-new-project command is used to create a new directory, but what does cd $_ do?

like image 598
Mukund Kumar Avatar asked May 10 '15 17:05

Mukund Kumar


People also ask

What does cd mean in command prompt?

The cd command, also known as chdir (change directory), is a command-line shell command used to change the current working directory in various operating systems.

What does cd mean Linux?

In Linux 'cd' (Change Directory) command is one of the most important and most widely used command for newbies as well as system administrators. For admins on a headless server, 'cd' is the only way to navigate to a directory to check log, execute a program/application/script and for every other task.

What is $_ in Linux?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.

What is cd in programming?

Continuous delivery (CD) is the automated delivery of completed code to environments like testing and development. CD provides an automated and consistent way for code to be delivered to these environments. Continuous deployment is the next step of continuous delivery.


2 Answers

$_ expands to the last argument to the previous simple command* or to previous command if it had no arguments.

mkdir my-new-project && cd $_ 

^ Here you have a command made of two simple commands. The last argument to the first one is my-new-project so $_ in the second simple command will expand to my-new-project.

To give another example:

echo a b; echo $_ #Will output: #a b #b 

In any case, mkdir some_dir && cd $_ is a very common combo. If you get tired of typing it, I think it's a good idea to make it a function:

mkdircd() {   #Make path for each argument and cd into the last path    mkdir -p "$@" && cd "$_"  } 

* The bash manual defines a simple command as "a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. " where control operator refers to one of || & && ; ;; ( ) | |& <newline>.

In practice$_ works like I've described but only with ||, &&, ;, or newline as the control operator.

like image 154
PSkocik Avatar answered Oct 02 '22 06:10

PSkocik


7. How to create directory and switch to it using single command. As you might already know, the && operator is used for executing multiple commands, and $_ expands to the last argument of the previous command.

Quickly, if you want, you can create a directory and also move to that directory by using a single command. To do this, run the following command:

$ mkdir [dir-name] && cd $_ 

For those coming from Udacity's Version Control with Git, HowToForge offers a great explanation, here.

like image 23
The Wall Street Baron Avatar answered Oct 02 '22 07:10

The Wall Street Baron