syntax error near unexpected token ' - bash

user3155779

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 !

jdt

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

bash: syntax error near unexpected token `('

From Dev

Bash script: syntax error near unexpected token?

From Dev

Bash syntax error near unexpected token `('

From Dev

bash: syntax error near unexpected token `('

From Dev

-bash: syntax error near unexpected token

From Dev

Bash 'source': syntax error near unexpected token `then'

From Dev

bash: syntax error near unexpected token `}'

From Dev

-bash: syntax error near unexpected token `;'

From Dev

bash: syntax error near unexpected token `('

From Dev

Bash : syntax error near unexpected token ' done '

From Dev

Bash: syntax error near unexpected token `else'

From Dev

bash: syntax error near unexpected token

From Dev

Bash script: syntax error near unexpected token?

From Dev

Bash - syntax error near unexpected token `fi'

From Dev

bash: syntax error near unexpected token `-o'

From Dev

BASH: syntax error near unexpected token `done'

From Dev

bash: syntax error near unexpected token `do'

From Dev

bash: syntax error near unexpected token '<'

From Dev

BASH - Syntax error near unexpected token 'done'

From Dev

bash: syntax error near unexpected token `;'

From Dev

bash: syntax error near unexpected token `foo'

From Dev

Bash script: syntax error near unexpected token `('

From Dev

-bash: syntax error near unexpected token '('

From Dev

"syntax error near unexpected token `elif'" error in bash

From Dev

Bash script error: line 167: syntax error near unexpected token 'then'

From Dev

Syntax error near unexpected token `('

From Dev

Syntax error near unexpected token 'then'

From Dev

Syntax error near unexpected token `='()

From Dev

syntax error near unexpected token `<'

Related Related

HotTag

Archive