46

I am getting weird behavior with the jobs, fg and bg commands in my zsh shell. Here is an example (this happens for all commands, not just python):

$ python &
[1] 21214
Python 2.7.8 (default, Oct 19 2014, 16:02:00)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
[1]  + 21214 suspended (tty output)  python
$ jobs
[1]  + suspended (tty output)  python
$ fg 1
fg: job not found: 1
$ bg 1
bg: job not found: 1

I am using the standard oh-my-zsh installation on OS X.

Ben Sandler
  • 2,223
  • 5
  • 26
  • 36

1 Answers1

87

You may be used to fg N (where N is a job number) working in Bash. But it’s a little different in Zsh, requiring a %; e.g., fg %1. The Bash behavior is convenient, so you can make Zsh do the same:

fg() {
    if [[ $# -eq 1 && $1 = - ]]; then
        builtin fg %-
    else
        builtin fg %"$@"
    fi
}

The same can be done for bg and history. This was originally from this thread.

You can also just type fg and the %1 is implied. Tab-completion is great for this too when you have a few jobs going: fg<tab>

Micah Elliott
  • 9,600
  • 5
  • 51
  • 54