Questions tagged [exit]

205 questions
490
votes
19 answers

Get exit status of process that's piped to another

I have two processes foo and bar, connected with a pipe: $ foo | bar bar always exits 0; I'm interested in the exit code of foo. Is there any way to get at it?
Michael Mrozek
  • 91,316
  • 38
  • 238
  • 232
112
votes
8 answers

Preventing grep from causing premature termination of "bash -e" script

This script does not output after: #!/bin/bash -e echo "before" echo "anything" | grep e # it would if I searched for 'y' instead echo "after" exit I could solve this by removing the -e option on the shebang line, but I wish to keep it so my…
iago-lito
  • 2,581
  • 4
  • 20
  • 35
88
votes
4 answers

Can I somehow add a "&& prog2" to an already running prog1?

Most shells provide functions like && and ; to chain the execution of commands in certain ways. But what if a command is already running, can I still somehow add another command to be executed depending on the result of the first one? Say I ran $…
us2012
  • 943
  • 6
  • 6
82
votes
4 answers

Default exit code when process is terminated?

When a process is killed with a handle-able signal like SIGINT or SIGTERM but it does not handle the signal, what will be the exit code of the process? What about for unhandle-able signals like SIGKILL? From what I can tell, killing a process with…
Cory Klein
  • 18,391
  • 26
  • 81
  • 93
70
votes
4 answers

Why does ( exit 1 ) not exit the script?

I have a script, that does not exit when I want it to. An example script with the same error is: #!/bin/bash function bla() { return 1 } bla || ( echo '1' ; exit 1 ) echo '2' I would assume to see the output: :~$ ./test.sh 1 :~$ But I…
Minix
  • 5,735
  • 8
  • 28
  • 45
67
votes
1 answer

Is there a standard command that always exits with a failure?

I want to test my script with a command that fails. I could use an existing command with bad arguments. I could also write a simple script that immediately exits with a failure. Both of these are easy to do and work for me, but if there is a…
Matthew
  • 5,417
  • 4
  • 19
  • 11
53
votes
4 answers

Is there any UNIX variant on which a child process dies with its parent?

I have been studying the Linux kernel behaviour for quite some time now, and it's always been clear to me that: When a process dies, all its children are given back to the init process (PID 1) until they eventually die. However, recently, someone…
John WH Smith
  • 15,500
  • 6
  • 51
  • 62
46
votes
6 answers

exit shell script from a subshell

Consider this snippet: stop () { echo "${1}" 1>&2 exit 1 } func () { if false; then echo "foo" else stop "something went wrong" fi } Normally when func is called it will cause the script to terminate, which is…
Ernest A C
  • 739
  • 3
  • 7
  • 9
46
votes
2 answers

Exit code at the end of a bash script

I am confused about the meaning of the exit code in the end of a bash script: I know that exit code 0 means that it finished successfully, and that there are many more exit codes numbers (127 if I'm not mistaken?) My question is about when seeing…
soBusted
  • 621
  • 2
  • 7
  • 12
43
votes
4 answers

How do I get the list of exit codes (and/or return codes) and meaning for a command/utility?

Is there a way I can do what stated in the title from the terminal commands, or will I have to look into the codes?
rusty
  • 1,841
  • 6
  • 21
  • 25
36
votes
3 answers

In Bash, how can I detect if I'm in a subshell?

I'm trying to write a function to replace the functionality of the exit builtin to prevent myself from exiting the terminal. I have attempted to use the SHLVL environment variable but it doesn't seem to change within subshells: $ echo $SHLVL 1 $ (…
jesse_b
  • 35,934
  • 12
  • 91
  • 140
35
votes
6 answers

Exit Google Chrome from terminal

Is there a way to cause google-chrome to quit, from the terminal, besides using killall google-chrome? I would like to be able to close it from a script without killing it.
slybloty
  • 1,228
  • 3
  • 17
  • 38
31
votes
7 answers

exit out of all SSH connections in one command and close PuTTY

Is there a way to back out of all SSH connections and close PuTTY in "one shot"? I work in Windows 7 and use PuTTY to SSH to various Linux hosts. An example of the way I find myself working: SSH to host1 with PuTTY... banjer@host1:~> #...doin some…
Banjer
  • 2,870
  • 6
  • 27
  • 35
30
votes
4 answers

bash -e exits when let or expr evaluates to 0

I have a bash script that sets -e so the script will exit on any exit status != 0. I'm trying to do some basic shell arithmetic assigned to variables and sometimes the expression equals 0 which causes the exit status of the let or expr command to be…
Dougnukem
  • 403
  • 4
  • 6
27
votes
4 answers

New parent process when the parent process dies

In UNIX, when a parent process disappears, I thought that all child processes reset init as their parent. Is this not correct all the time? Are there any exceptions?
user100503
  • 447
  • 1
  • 4
  • 8
1
2 3
13 14