9

In bash, if I run kill %1, it kills a backgrounded command in the current shell (the most recent one, I believe).

Is there an equivalent of this in fish? I haven't been able to find it online in a bit of web searching.

I'm not sure if I did it wrong, but

$ ps
    PID TTY          TIME CMD
  73911 pts/5    00:00:00 fish
  73976 pts/5    00:00:00 ps
$ sleep 100
^Z⏎
$ kill %1
$ ps
    PID TTY          TIME CMD
  73911 pts/5    00:00:00 fish
  74029 pts/5    00:00:00 sleep
  74121 pts/5    00:00:00 ps
5
  • It's not explicit in the current fish docs, but in v2.6 there's "if the string is the ID of a job, the result is the process group ID of the job" with the example "kill -s SIGINT %3 will send the SIGINT signal to the job with job ID 3."
    – Jeff Schaller
    Commented Jul 27, 2020 at 1:03
  • I just ran fish to test this, and it worked as expected. Can you show an example where it didn't work as expected? Maybe include version information, too.
    – Jeff Schaller
    Commented Jul 27, 2020 at 1:26
  • In fish, have a read of man jobs. It's short, so easy to read. It sounds like it contains what you need.
    – CJK
    Commented Jul 27, 2020 at 7:10
  • I added an example where it didn't seem to work
    – k_g
    Commented Jul 27, 2020 at 7:51
  • 1
    Best not to make assumptions about the ordering of job ids in any shell. Your bash conjecture is certainly invalid -- it would involve renumbering every other running job as each new job starts. Commented Jul 27, 2020 at 9:13

2 Answers 2

9

Your command has worked, but because the job is stopped it has not responded to the signal.

From your example where it didn't seem to work, try continuing the process with fg or bg, or forcibly terminate the process with kill -SIGKILL %1, and it will exit.

kill %1 works immediately in bash and zsh because it is a builtin command in these shells and sends SIGCONT in addition to SIGTERM (or the specified signal).

0

The jobs fish command can show the id/group of the command jobs. To get the latest job id you want to run (jobs -gl) emulates the %1 in zsh/bash

hence

kill (jobs -lg)

will do the trick

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .