What does “cd &” mean?

I know how to change directory from terminal by typing

cd <directory>

but here’s the scenario. I was using terminal to go on my git repository’s folder.

  • glennRepositories
    • glenn-remdroid-repository

Given the folder structure above, I want to switch in glenn-remdroid-repository because it is my repository by doing.

cd glennRepositories

Before switching in glenn-remdroid-repository, I mistakenly typed the commmand

cd & 

instead of cd *. I used cd * because the folder name is long for me, sorry for being lazy.

When I execute the cd & , the terminal replies

[1] 7519

When I execute cd * this is now happened

[1]+  Done                    cd  (wd: ~/glennRepositories)
(wd now: ~/glennRepositories/glenn-remdroid-repository)

Does anyone what cd & means and its output? Google doesn’t know my question 🙂

Answer

By writing the & on the end of the command line, you are instructing your shell to take the command pipeline (in this case a single command) and run it in a sub-shell in the background (not waiting for a result). cd is a bash builtin, but that is not important here. It will not affect your “parent” shell in any way, so your working directory will not be changed.

[1] 7519

This line is reporting that there is a job running in the background identified by number 1 and the process ID is 7519. A similar thing will be printed if you run e.g. vim and then press Ctrl+Z to suspend it. When the process finishes, the shell won’t report that until the next processing cycle (e.g. just hit Return once again) and then it will print the update listing jobs finished.

For further details please read man bash and the section JOB CONTROL.

For reference, the question Why does exit & not work? bears some similarity.

Attribution
Source : Link , Question Author : GLenn , Answer Author : Toby Speight

Leave a Comment