0

I understand similar questions to this been asked in SO multiple times. However, I couldn't find any usual suspects here.

#!/bin/bash
myvar="test"
if [ "$myvar" == "test" ]
then
echo "Test mode"
fi

Spent quite some time on it. Can anyone advice what am I missing?

I am able to execute the script, but couldn't source the same.

error while source bash: test.sh: line 7: syntax error: unexpected end of file

$ which bash
/bin/bash
$ bash --version
3.2.57(1)

I am able to get the same working on my another Mac. So, it's pretty much something wrong on my Mac, but couldn't figure out what it is. Also, not only the above-mentioned script, any script with "if" condition I couldn't source. Tried different examples mentioned here, same syntax error.

edit1:

$ file test.sh 
test.sh: Bourne-Again shell script text executable, ASCII text

edit2:

$ hexdump -C test.sh
    00000000  23 21 2f 62 69 6e 2f 62  61 73 68 0a 6d 79 76 61  |#!/bin/bash.myva|
    00000010  72 3d 22 74 65 73 74 22  0a 69 66 20 5b 20 22 24  |r="test".if [ "$|
    00000020  6d 79 76 61 72 22 20 3d  3d 20 22 74 65 73 74 22  |myvar" == "test"|
    00000030  20 5d 0a 74 68 65 6e 0a  65 63 68 6f 20 22 54 65  | ].then.echo "Te|
    00000040  73 74 20 6d 6f 64 65 22  0a 66 69 0a              |st mode".fi.|
    0000004c
15
  • Works fine for me. Commented Apr 17, 2019 at 6:07
  • Did you make a editor in Windows to create this script and run it on Linux? Commented Apr 17, 2019 at 6:08
  • @John1024 -> works for me too in my other machine Commented Apr 17, 2019 at 6:13
  • @Inian -> I am just using VScode or vi on my mach Commented Apr 17, 2019 at 6:13
  • 2
    Possible duplicate of Bash syntax error: unexpected end of file Commented Apr 18, 2019 at 17:58

4 Answers 4

1

Check for newline character differences or encoding pages. if it something as simple as this and there is a mac involved, always check that first

Sign up to request clarification or add additional context in comments.

1 Comment

no issues with newline. Not just this script, I couldn't source any script with an "if" condition. file -I test.sh test.sh: text/x-shellscript; charset=us-ascii Is there any way else I can find?
1

As other users already stated, you can check if newline is LF because CRLF will fail in linux. if you convert the script it should be fixed. you can install a tool which can convert CRLF into LF:

dos2unix test.sh

Regarding the sourcing, not sure if this may help you with your issue. assuming the wrapper script which sources test.sh is not called from bash...

#!/bin/sh
. ./test.sh

...the if statement will fail, because bourne shell does not support '==' but if you change it to...

#!/bin/bash
myvar="test"
if [ "$myvar" = "test" ]

...the script will sourced without errors. Please note it depends on how the script is sourced, the shebang might be ignored

3 Comments

Yeah tried dos2unix, but no help I am just trying source test.sh. (not calling the script inside a wrapper)
can you please update question with output of hexdump -C test.sh
source test.sh will look for a file in PATH. Specify the full path to test.sh to make sure you're sourcing the right one
1

Okay. After 10 months, my wife helped me debug this problem.

I had an alias in .bash_profile alias fi='firebase init'

Alias name fi makes bash to confuse with if statement.

Comments

0

Okay finally, found the issue.

I removed my .bashrc file and its working fine. Looks like something in .bashrc was messing it.

$ source test.sh
Test mode

Thanks, everyone appreciate it.

1 Comment

Can you share the offending .bashrc? Without access to that, there's a million different things which could be wrong, and I'm guessing it's something really obscure. (Reason I'm asking: OP begging in chat.)

Your Answer

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.