8

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 :)

  • 1
    Something that can't be found with Google is ogooglebar, a Swedish neologism. – Chloe Aug 23 '13 at 1:21
  • When I type my question, the search result was about Compact disk. Ungoogleable :) – GLenn Aug 23 '13 at 1:25
  • 3
    " I used cd * because the folder name is long for me, sorry for being lazy". Simply type cd <TAB> and the name of the unique folder will be expanded automatically. – Bakuriu Aug 23 '13 at 6:55
20

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.

  • Whether the job end is reported straight away or at the next prompt depends on the shell. In fish and zsh, it's reported straight away. In bash and other POSIX shells or (t)csh it depends on the setting of the notify option (off by default) (zsh being a POSIX-like shell also has a notify option, but it's off by default unless it's told to emulate another shell) – Stéphane Chazelas Feb 27 '17 at 9:55
1

The & operator sends a process to background; as cd can be run without arguments you are running it and sending it to background. If you take the man page:

NAME

cd - Change the shell working directory.

SYNOPSIS

cd [-L|-P] [dir]

DESCRIPTION

Change the shell working directory.

Change the current directory to DIR. The default DIR is the value of the HOME shell variable.

The variable CDPATH defines the search path for the directory containing DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins with a slash (/), then CDPATH is not used.

If the directory is not found, and the shell option cdable_vars is set, the word is assumed to be a variable name. If that variable has a value, its value is used for DIR.

Options:

-L force symbolic links to be followed
-P use the physical directory structure without following symbolic links

The default is to follow symbolic links, as if -L were specified.

Exit Status:

Returns 0 if the directory is changed; non-zero otherwise.

SEE ALSO

bash(1)

IMPLEMENTATION

GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later

You can see the arguments are optional. So

cd & is processed as cd and then send to background. When cd is called with no argument no processing is needed. So it goes to background as "done" (processed).

Some info regarding & The Bash & (ampersand) is a builtin control operator used to fork processes. From the Bash man page, "If a command is terminated by the control operator &, the shell executes the command in the background in a subshell".

If logged into an interactive shell, the process is assigned a job number and the child PID is displayed. The job number below is one.

bash$ sleep 30 &
[1] 3586

Note that when a process is forked, the child PID is stored in the special variable $!

bash$ echo $!
3586

You can terminate the job by its job number like so:

bash$ jobs
[1]+ Running sleep 30 &
bash$ kill %1
[1]+ Terminated sleep 30
bash$
  • 5
    Close, but not quite. First, it's not that cd is run and then sent to the background. The background process is actually created before the program runs in the case of a regular command; in the case of a built-in, this behavior is emulated. The command gets run by this background process, replacing it. Second, cd without arguments does do something: it takes you to your HOME directory. But, since you've forked it off into the background, there is no effect in this case. – Warren Young Aug 23 '13 at 0:52
  • @WarrenYoung from the manpage it says cd will take the home directory if no value is assigned to it. Upon sucessful directory change doesnt it recieve the value of $pwd? – vfbsilva Aug 23 '13 at 0:55
  • 1
    The background process's current working directory gets affected, not the foreground Bash shell's. – Warren Young Aug 23 '13 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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