1

I am using shellcheck to fix warnings in a sh script. The script checks for the hard fd limit with:

ulimit -H -n

It sets the soft fd limit with:

ulimit -n "$MAX_FD"

This generates the shellcheck warnings:

In POSIX sh, ulimit -H is not supported. [SC2039]

In POSIX sh, ulimit -n is not supported. [SC2039]

What is the portable way to both get the hard resource limit and set the soft resource limit in POSIX sh?

2

Well, strictly speaking, the POSIX ulimit only seems to know the -f option (max file size), so I suppose the answer is "none".

In practice, all shells I have on Linux (Bash, Busybox, Dash, Ksh, mksh, yash, zsh), have a builtin ulimit that supports -H, -S, and -n.

I don't know about non-Linux systems for sure, but the FreeBSD man page for sh(1) also mentions those, and on OpenBSD, /bin/sh appears to be ksh, which also supports them.

  • so in practice just ignore this warning without ill effect? what about scripts that may run on BSDs? – Dylan Cali Mar 12 at 17:11
  • 1
    @DylanCali, looks like something you'll have to check on each OS. Or just use Bash/ksh or such. The underlying system calls are in POSIX, so I suppose the shells that support -HSa it would support them on all OSes. Maybe. – ilkkachu Mar 12 at 17:15
  • came across this openbsd thread from several years ago where Theo de Raadt indicates ksh ulimit semantics and this reddit thread that indicates on openbsd sh is really just ksh in disguise... so seems to be safe to assume these options there as well – Dylan Cali Mar 12 at 17:22
  • It is important to read the second paragraph of the OpenBSD sh manual. – JdeBP Mar 12 at 18:00
  • @JdeBP, well, that's of course one way of writing documentation. My fault for just grepping to the point. Thanks. – ilkkachu Mar 12 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.