"set: illegal option -" on one host but not the other
Asked Answered
D

7

21

I've written a sh script in one of my ubuntu VMs which works fine, but when I try to run it in my other VMs, it does not work. Both VMs should be the same. With bash --version both VMs reply with:

GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

With lsb_release -a, both also reply:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.3 LTS
Release:        14.04
Codename:       trusty

My security_steps.sh script looks like this:

#!/bin/sh

set -e

if ! [ -f svn_up_action.sh ]; then
  echo "svn_up_action.sh is missing. Please make sure it is in the same directory as this script."
  exit
fi

When I do: sudo sh security_steps.sh, the console errors with:

: not foundeps.sh: 6: security_steps.sh:
security_steps.sh: 7: set: Illegal option -

How can I figure out what's going on with the VM of the non-working shell? I feel like the shells are somehow different. I appreciate your help!

Danyluk answered 12/2, 2016 at 0:53 Comment(6)
This isn't a bash script, so the question shouldn't be tagged bash. /bin/sh is POSIX sh, not bash.Anguish
Also, your probable underlying problem is the very first thing that the tag wiki at stackoverflow.com/tags/bash/info tells you to check before asking.Anguish
...similarly, the output of bash --version is completely irrelevant to a script that uses #!/bin/sh rather than #!/bin/bash.Anguish
@CharlesDuffy Thank you so much for that link! Yes there are ^M at the end of each lineDanyluk
Did using dos2unix, the vim :set fileformat=unix, or a similar command resolve your problem successfully?Anguish
@CharlesDuffy Yes, dos2unix solved my issue. Thank you.Danyluk
A
52

This almost certainly means your file has DOS newlines -- thus, hidden CR characters at the end.

Thus, set -e becomes set -e$'\r' (using bash-specific syntax to represent the CR character), which isn't a valid option.

This also explains the : not found, as a CR will reset the cursor to the beginning of the line, truncating an error message of the form sh: commandname: not found by making the commandname instead an operation that moves the cursor to the beginning of the line.

Anguish answered 12/2, 2016 at 0:57 Comment(1)
This is the underlying solution to the problem and this answer should be marked as accepted. I came through this problem and took about 2 hours to realize that a simple edit with VsCode on my script was placing that CR characters at the end.Rabblerousing
S
12

I had *.sh file throwing me this while running it on Windows (via GitBash).

I've had to change line endings to UNIX/OSX format.

Salvador answered 9/5, 2016 at 5:45 Comment(1)
This is the same thing the answer preceding it by three months said, no?Anguish
R
11

In Windows:

choco install dos2unix
dos2unix file.sh

In Linux:

sudo apt install dos2unix
dos2unix file.sh

dos2unix converts the file to the Unix type.

After doing this, try to run the application again or the file.

Ranged answered 7/10, 2020 at 1:10 Comment(0)
S
9

If you run into this problem because you are touching your files on a Windows machine, Notepad++ is your friend.

Inspection
View > Show Symbol > Show All Characters
Look for CR LF line endings, rather than LF line endings!

Solving
Search > Replace (Ctrl+H)
Make sure Search Mode is set to Extended
Find \r\n and replace with \n

Selves answered 4/6, 2021 at 11:58 Comment(0)
L
1

You can use the below command, it will remove the hidden CR characters

sed -i 's/\r$//' filename
Livable answered 13/8, 2022 at 6:54 Comment(0)
H
1

it is because of End of Line Sequence (CRLF and LF) The root cause is the Windows use \r\n as a end-of-line, while the *nix use only \n.

In Windows set the End of Line Sequence on LF.

You can select it easily in VSCode.

Huguenot answered 16/12, 2023 at 22:54 Comment(1)
i used prettier to add new line at the end of the file in vscode & it set it to CRLF. once i changed it to LF, it worked. this was in docker bdw with run.sh script at the end using CMD.Moton
K
0

I got the same Error,

Later I found that these raise because My shell script file moved from a windows machine to Linux machine(there may be a chance of formating or Encoding error).

So I have created the document in Linux machine itself. It solved the issue.

This "May" help few, I am posting this because it worked for me.

Kain answered 8/6, 2020 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.