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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Am trying to write a one-liner that can probe the output of df -h and alert when one of the partitions is out [or almost] of space. It's the part using xargs that kicking me in the ass now...

echo 95 | xargs -n1 -I{} [ {} -ge 95 ] && echo "No Space on disk {}% full -- remove old backups please"

How can i make the second {} show "95" too?

share|improve this question
up vote 10 down vote accepted

That && is not part of the xargs command, it's a completely separate invocation. I think you'll want to explicitly execute a subshell:

echo 95 | xargs -n1 -I_percent -- sh -c '[ _percent -ge 95 ] && echo "No Space on disk _percent% full -- remove old backups please"'

Note also I'm using _percent instead of {} to avoid extra quoting headaches with the shell. It's not a shell variable; still just an xargs replacement string.

share|improve this answer
this is neat. thanks – nemesisfixx Nov 26 '12 at 12:37

An alternative way, which is more readable, is to define a separate function which contains all your other commands and then call that function with xargs in a sub-shell.

Hence, for example:

  [ "$1" -ge 95 ] && echo "No Space on disk $1% full -- remove old backups please"
  echo "Another command echoing $1"

export -f myfunc

echo 95 | xargs -n1 -I_percent -- sh -c 'myfunc "_percent"'
share|improve this answer

Your Answer


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.