Skip to main content
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 263
Source Link
gfd
  • 31
  • 2

Using the value of a variable as an exit code from a script

I can find plenty of information about how to assign the exit code of a script to av variable, but I want to do the opposite: use the value of a variable (defined within a script) as the exit code of the script.

For example, I have a script that couunts the number of open files on a specific file system (plus a bunch of other stuff). I want the exit code of the script to be the number of open files, so essentially:

#!/bin/bash

OPENFILES=`lsof /filesystem | wc -l`

exit $OPENFILES

So after the script exits, 'echo $?' will give me the value of $OPENFILES that was defined in the script.

I've tried various permutations of using quotes, parentheses, etc, but come up empty. I feel like what I'm trying to do is either impossible, or so blindingly obvious that I'm overlooking it...