Take the 2-minute tour ×
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.

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

share|improve this 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

2 Answers 2

up vote 16 down vote accepted

By saying the & on the end of command line you are instructing your shell to take set of commands and run them in sub-shell on background (not waiting for a result). cd is the bash builtin but it is not important here. It will not affect your "parent" shell in any way, so your working directory will not be changed.

[1] 7519

Is reporting that there is a job running on the background identified by number 1 and the process ID is 7519. Similar thing will be printed if you run eg. vim and then press Crtl+Z. Then when the process finishes it will actually not report it by itself. Rather, it waits for next processing cycle (eg. just hit Return once again) and then it will print update on jobs finished.

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

For reference this (Why does `exit &` not work?) question bares some similarity.

share|improve this answer

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

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 ads "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$
share|improve this answer
2  
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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