Aviator
Aviator

Reputation: 734

syntax error near unexpected token `{​ - bash

​​​​​defineColumns() {
​​​​​​​    shift
    local dirfun=${​​​​​​​​1:-"/var/log/was/dial"}​​​​​​​​
    local basefun=${​​​​​​​​2:-"$logdir/party_info.$(date +%y%m%d%H%M%S)"}​​​​​​​​
    touch $logbase_bcdb
    info $dirfun $basefun
    # Run steps sequentially
    loadData
}

I am writing a shell script code in which I have written multiple functions. The above function is throwing error as : syntax error near unexpected token `{​ What is wrong in the code?

Upvotes: 0

Views: 405

Answers (1)

wjandrea
wjandrea

Reputation: 33159

There are non-printing characters in the code: Unicode U+200B ZERO WIDTH SPACE. Remove them and you should be fine.

Firstly to see them, you could use cat -A but it shows these characters as M-bM-^@M-^K, which is confusing IMO. I'd rather read the Python ascii() representation, so here's a quick script:

import fileinput

for line in fileinput.input():
    print(ascii(line))

Save that as ascii_lines.py then run with the name of your script:

$ python3 ascii_lines.py filename.sh
'\u200b\u200b\u200b\u200b\u200bdefineColumns() {\n'
'\u200b\u200b\u200b\u200b\u200b\u200b\u200b    shift\n'
'    local dirfun=${\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b1:-"/var/log/was/dial"}\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\n'
'    local basefun=${\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b2:-"$logdir/party_info.$(date +%y%m%d%H%M%S)"}\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\n'
'    touch $logbase_bcdb\n'
'    info $dirfun $basefun\n'
'    # Run steps sequentially\n'
'    loadData\n'
'}\n'

Then to remove them, you could use sed, though it doesn't know Unicode, so I'm using a Bash $'' string here to solve that.

$ sed -i $'s/\u200b//g' filename.sh

Afterwards:

$ python3 ascii_lines.py filename
'defineColumns() {\n'
'    shift\n'
'    local dirfun=${1:-"/var/log/was/dial"}\n'
'    local basefun=${2:-"$logdir/party_info.$(date +%y%m%d%H%M%S)"}\n'
'    touch $logbase_bcdb\n'
'    info $dirfun $basefun\n'
'    # Run steps sequentially\n'
'    loadData\n'
'}\n'

Upvotes: 3

Related Questions