6

Taken from FreeBSD's man page on sh (because its the most convenient online, target platform is Debian if it matters):

SH(1)           FreeBSD General Commands Manual          SH(1)

NAME sh -- command interpreter (shell)

SYNOPSIS sh [-/+abCEefhIimnPpTuVvx] [-/+o longname] [script [arg ...]] sh [-/+abCEefhIimnPpTuVvx] [-/+o longname] -c string [name [arg ...]] sh [-/+abCEefhIimnPpTuVvx] [-/+o longname] -s [arg ...] ...

I'm particularly interested in the use case of:

sh [-/+abCEefhIimnPpTuVvx] [-/+o longname] -c string [name [arg ...]]

Example:

# Normal:
myscript hello world
myscript hello world >/var/log/myscript.combined 2>&1

Constrained:

- Arguments 'hello' and 'world' are suffixed by calling program.

myscript >/var/log/myscript.combined 2>&1 hello world

Constrained Further:

- Wrapped in sh to catch system messages, like Segmentation Fault

sh -c 'myscript $@' _ >/var/log/myscript.combined 2>&1 hello world

I noticed that the first argument was not passed to myscript, and the documentation alludes to a name parameter, that I didn't see a doc section on. In my example I have added ad _ in place of the name argument, but:

What should I fill for name?

Ricardo
  • 111
ThorSummoner
  • 4,422

2 Answers2

7

The form:

sh -c '...' name arg1 arg2 ...

is called inline-script, often seen in combination with find ... -exec sh -c '...' find-sh {} +. Inside inline-script, $0 will be set to name, and the rest of arguments was populated to $@.

In general, you should set it to something meaningful, because it will be used for instance showing error messages:

sh -c 'echo "${1?}"' foo
foo: 1: foo: 1: parameter not set

But you can set name to any word you like, to indicate the name of inline-script:

sh -c 'printf "%s\n" "$0"' custom-sh 1 2 3
custom-sh

This behavior is defined by POSIX.

cuonglm
  • 153,898
  • see also https://unix.stackexchange.com/q/152391/231113 for further details on the command_name argument – myrdd May 07 '18 at 14:31
5

Man page in Linux was detailed a bit.

sh -c [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name] [+o option_name] command_string [command_name [argument ...]]

-c Read commands from the command_string operand instead of from the standard input. Special parameter 0 will be set from the command_name operand and the positional parameters ($1, $2, etc.) set from the remaining argument operands.

command_name A string assigned to special parameter 0 when executing the commands in command_string. If command_name is not specified, special parameter 0 shall be set to the value of the first argument passed to sh from its parent (for example, argv[0] for a C program), which is normally a pathname used to execute the sh utility.

  • Hmmk, I tested with a php script and a python script, both of which seem to populate their own argv[0] dynamically. So perhaps this argument doesn't apply to my use case. I would like to know if it gets used anywhere like by the kernel as metadata to the process. – ThorSummoner Sep 02 '15 at 00:14