The error that you get is from the dash
shell (the shell used for implementing /bin/sh
on your system). It is due to $installRequiredFiles
being
- an empty string, and
- used unquoted.
Since the variable is empty, using it unquoted removes it completely from the command, which means that the line
if [ $installRequiredFiles = "true" ]; then
would be interpreted as
if [ = "true" ]; then
which in turn is an error in the usage of the [
command; it sees the operator =
when no operator was expected.
So, why is $installRequiredFiles
(and $1
), empty?
The command
sh -c ./myscript.sh "true"
runs the command ./myscript.sh
with $0
set to the string true
in the script. The value in $0
is usually the name of the script or of the shell, and the value is most commonly used in diagnostic messages (e.g. in error messages that the shell produces).
If you had used
sh -c ./myscript.sh sh "true"
instead, then $1
would have been set to test
as expected, and $0
would have been set to sh
(which is customary for in-line sh -c
scripts). In both cases, the script would have been executed by whatever shell executes scripts with no #!
-line. What shell ends up running the script is possibly depending on the sh
shell on your machine, and it may not be sh
or bash
. See, e.g., Which shell interpreter runs a script with no shebang?.
You likely want to add a #!
-line in your script pointing to /bin/sh
:
#!/bin/sh
# ... rest of script here ...
# ... just remember to quote all expansions ...
That means you'll be able to run your script like so:
$ ./myscript.sh test
or, with the equivalent
$ sh -c './myscript.sh "test"'
or,
$ sh -c './myscript.sh "$@"' sh test
Note that in the last two of these cases, it's not the sh -c
shell that is executing the script, but whatever shell the #!
-line in the script refers to, just as if you ran ./myscript.sh "test"
directly. The difference compared to before adding the #!
-line is that you now know for certain that this is /bin/sh
and not some other shell.
The script uses only POSIX shell syntax, which means it would be executable by /bin/sh
, regardless of what shell is used to implement /bin/sh
on any given system. If /bin/sh
is bash
, ksh
or dash
or some other more exotic shell does not matter, the user should not need to worry about running the script with the correct interpreter.
Related to various parts of this answer: