2

I'm working on a series of command lines to create files, set variables and such on Linux systems. I cannot tell which shell will actually be used during execution but I do know that it will be one of csh, tcsh, sh, ksh, or bash.

csh and tcsh not being bourne compatible is my main issue at this point since they use different commands/syntax for both setting variables and conditional expressions.

I could simply create a shell script with a shebang to switch to /bin/sh if it is safe to assume it being present.

Therefore I'm wondering: Is it safe to assume sh being installed and available or is there another more elegant way to handle the different shell styles without creating seperate scripts?

Revoluzifer
  • 323
  • 4
  • 14
  • link to other question that may come close to answering yours [Recommended POSIX sh shebang](https://stackoverflow.com/a/53116875/13982210) ; [why the previous answer speaks of /usr/bin/env](https://stackoverflow.com/a/10383546/13982210) – kevinnls Jan 05 '22 at 11:28

1 Answers1

4

It is safe to assume that there is a sh in POSIX compatible systems, but its path may vary. The Open Group specification says to determine the location of the standard sh utility with:

command -v sh

So you could do something like:

"`command -v sh`" <<EOS
echo my super sh script
EOS

But there's a problem: csh doesn't support quoted heredocs, which will make the process of writing the script a real pain.

As you're targeting Linux, you might directly write a sh script and use the shebang #!/bin/sh or #!/usr/bin/env sh.

Take notice that the variables set in the script won't be available to the parent shell. For that you need to source the script, in which case the syntax will matter and you'll have to write at least both sh and csh versions of it.

Fravadona
  • 13,917
  • 1
  • 23
  • 35