17

I have a written a sample script on my Mac

#!/bin/bash
test() {
 echo "Example"
}
test
exit 0

and this works fine by displaying Example

When I run this script on a RedHat machine, it says

syntax error near unexpected token '

I checked that bash is available using

cat /etc/shells
which bash shows /bin/bash 

Did anyone come across the same issue ?

Thanks in advance !

Tomasz Jakub Rup
10.7k7 gold badges51 silver badges49 bronze badges
asked Jan 3, 2014 at 3:20
8
  • hmm I don't know. I can execute this correctly on a Fedora machine. Commented Jan 3, 2014 at 3:25
  • Any thing before definition works fine, error is being thrown when control reaches function definition. Commented Jan 3, 2014 at 3:26
  • 2
    Can you recreate the entire file? Perhaps you have a non-printable in there causing the fault. Commented Jan 3, 2014 at 3:28
  • 4
    @user3155779 Could you close the question, since the actual problem didn't relate to anything asked here (that is, wasn't found in the code as-given byte-for-byte)? Commented Jan 3, 2014 at 4:47
  • 3
    This question appears to be off-topic because the solution was unrelated to the question as asked. Commented Jan 3, 2014 at 16:23

6 Answers 6

36

It could be a file encoding issue.

I have encountered file type encoding issues when working on files between different operating systems and editors - in my case particularly between Linux and Windows systems.

I suggest checking your file's encoding to make sure it is suitable for the target linux environment. I guess an encoding issue is less likely given you are using a MAC than if you had used a Windows text editor, however I think file encoding is still worth considering.

--- EDIT (Add an actual solution as recommended by @Potatoswatter)

To demonstrate how file type encoding could be this issue, I copy/pasted your example script into Notepad in Windows (I don't have access to a Mac), then copied it to a linux machine and ran it:

jdt@cookielin01:~/windows> sh ./originalfile 
./originalfile: line 2: syntax error near unexpected token `$'{\r''
'/originalfile: line 2: `test() {

In this case, Notepad saved the file with carriage returns and linefeeds, causing the error shown above. The \r indicates a carriage return (Linux systems terminate lines with linefeeds \n only).

On the linux machine, you could test this theory by running the following to strip carriage returns from the file, if they are present:

cat originalfile | tr -d "\r" > newfile

Then try to run the new file sh ./newfile . If this works, the issue was carriage returns as hidden characters.

Note: This is not an exact replication of your environment (I don't have access to a Mac), however it seems likely to me that the issue is that an editor, somewhere, saved carriage returns into the file.

--- /EDIT

To elaborate a little, operating systems and editors can have different file encoding defaults. Typically, applications and editors will influence the filetype encoding used, for instance, I think Microsoft Notepad and Notepad++ default to Windows-1252. There may be newline differences to consider too (In Windows environments, a carriage return and linefeed is often used to terminate lines in files, whilst in Linux and OSX, only a Linefeed is usually used).

A similar question and answer that references file encoding is here: bad character showing up in bash script execution

answered Jan 5, 2014 at 2:08

6 Comments

What part of Windows defaults to UTF-16LE? Note that ISO-8859-1 is the same as Windows-1252, which isn't particularly endorsed by ANSI as opposed to Unicode. Also, none of this will make a quote mark show up in an otherwise ASCII file, so -1.
@Potatoswatter, Windows uses UTF-16 'internally' so its fair to point out that the system does not default everything that way and operating system internals I guess are not relevant to the point I was trying to make, which is that filetype encoding can cause these side-effects. Windows-1252 is a superset of ISO-8859-1, thus they are not the same. The point of my answer is that filetype encoding is often a consideration between operating systems and applications. I have updated the answer, hopefully my point is a little better represented now.
Ah, I didn't realize 1252 had characters instead of control codes for 0x80-0x9F. In that case, there's nothing ANSI about it.
I suspect the OP's issue was encoding though. I just copied the example into notepad and saved, then copied to linux and sh ./file produces unexpected token errors, including `' type references. To fix I needed to strip the carriage returns. i.e. $ cat originalfile | tr -d "\r" > newfile then it would execute. Anyway, I agree my answers a little verbose.
It's not the verboseness, it's the unspecificity ("doing this general kind of thing can cause various types of errors") and factual errors ("ANSI/Windows-1252"). If you reproduced or fixed the problem, you should mention so. It sounds like the shell was complaining about the ^M carriage return character, not the quote as the OP seemed to indicate.
|
5

try something like

$ sudo apt-get install dos2unix
$ dos2unix offendingfile
answered Sep 17, 2014 at 14:05

1 Comment

Mac uses Unix line endings. This is unlikely to help.
4

Easy way to convert example.sh file to UNIX if you are working in Windows is to use NotePad++ (Edit>EOL Conversion>UNIX/OSX Format)

You can also set the default EOL in notepad++ (Settings>Preferences>New Document/Default Directory>select Unix/OSX under the Format box)

answered Aug 4, 2016 at 19:11

Comments

2

Thanks @jdt for your answer.

Following that, and since I keep having this issue with carriage return, I wrote that small script. Only run carriage_return and you'll be prompted for the file to "clean".

https://gist.github.com/kartonnade/44e9842ed15cf21a3700

alias carriage_return=remove_carriage_return
remove_carriage_return(){
# cygwin throws error like : 
# syntax error near unexpected token `$'{\r''
# due to carriage return
# this function runs the following
# cat originalfile | tr -d "\r" > newfile
read -p "File to clean ? "
file_to_clean=$REPLY
temp_file_to_clean=$file_to_clean'_'
# file to clean => temporary clean file
remove_carriage_return_one='cat '$file_to_clean' | tr -d "\r" > '
remove_carriage_return_one=$remove_carriage_return_one$temp_file_to_clean
# temporary clean file => new clean file
remove_carriage_return_two='cat '$temp_file_to_clean' | tr -d "\r" > '
remove_carriage_return_two=$remove_carriage_return_two$file_to_clean
eval $remove_carriage_return_one
eval $remove_carriage_return_two
# remove temporary clean file 
eval 'rm '$temp_file_to_clean

}

answered May 14, 2014 at 8:27

Comments

0

I want to add to the answer above is how to check if it is carriage return issue in Unix like environment (I tested in MacOS)

1) Using cat

cat -e my_file_name

If you see the lines ended with ^M$, then yes, it is the carriage return issue.

2) Find first line with carriage return character

grep -r $'\r' Grader.sh | head -1

3) Using vim

vim my_file_name

Then in vim, type

:set ff

If you see fileformat=dos, then the file is from a dos environment which contains a carriage return.

After finding out, you can use the above mentioned methods by other people to correct your file.

answered Jun 4, 2018 at 19:18

Comments

0

I had the same problem when i was working with armbian linux and Windows . i was trying to coppy my codes from windows to armbian and when i run it this Error Pops Up. My problem Solved this way : 1- try to Coppy your files from windows using WinSCP . 2- make sure that your file name does not have () characters

answered Jul 24, 2019 at 7:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.