There are two problems here. First, while you have a shebang line calling bash, you are executing the script as sh script.sh
. The shebang line already specifies an interpreter for this script. So, just make the script executable:
chmod a+x ./test.sh
And then run it:
./test.sh
Alternatively, run it with bash explicitly:
bash ./test.sh
That will make it run with bash
instead of sh
which is a more limited shell and doesn't understand ==
. Which brings us to the second problem and why this fails. Strangely enough, you want eq
or =
, not ==
. The operator that tests numerical equality in POSIX shell scripts (sh
is a POSIX-compliant shell) is -eq
while the one that tests string equality is =
and ==
isn't a thing.
So, if you want your script to be run by sh
, change it to:
if [ 1 = 1 ]; then
echo "Something"
fi
Which checks that the string 1
is the same as the string 1
, or to compare numerically, use:
if [ 1 -eq 1 ]; then
echo "Something"
fi
In addition, you have another problem. I can only reproduce the specific error you get in dash
if I add a \r
after the then
. This is a classic problem when moving between Windows and Linux. While Linux uses \n
as the line end character, Windows ends its lines with \r\n
. This \r
character is not visible to the user, but it is there and confuses the script. So, you can remove it with:
sed -i 's/\r//' ./test.sh
And in the future, use a proper text editor to write your scripts. Either one that works in the WSL system, or one that can at least be configured to not add \r
to the ends of its lines.
[
is thetest
command, this has nothing to do with your script's file name. WSL is the “Windows Subsystem for Linux” I suppose you're talking about here: https://msdn.microsoft.com/de-de/commandline/wsl/about – dessert Nov 16 '17 at 08:59sh -x ./test.sh
andtype -a sh
, please – muru Nov 16 '17 at 09:43