When I source
the following simple script in a bash shell on macOS (Sonoma 14.4.1), I get
"syntax error: unexpected end of file".
#!/bin/bash
echo "Hello"
echo "$SHELL"
echo "$BASH_VERSION"
/bin/bash --version
if [[ -n "foobar" ]]; then
echo "Ok"
fi
echo "Bye"
If I knock out the if
-statement (lines 7-9), it behaves as expected. On Linux (CentOS 7), it (with the if
-statement) produces the expected output with "Ok".
I'm using Unix line endings (LF).
macOS result:
$ source bash-if-trouble.sh
Hello
/bin/bash
3.2.57(1)-release
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin23)
Copyright (C) 2007 Free Software Foundation, Inc.
-bash: bash-if-trouble.sh: line 12: syntax error: unexpected end of file
source ./bash-if-trouble.sh
produces a similar result.
CentOS7 result, which is also expected on macOS (modulo the version details, of course):
$ source bash-if-trouble.sh
Hello
/bin/bash
4.2.46(2)-release
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 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.
Ok
Bye
Per @Philippe's suggestion:
$ od -a bash-if-trouble.sh
0000000 # ! / b i n / b a s h nl e c h o
0000020 sp " H e l l o " nl e c h o sp " $
0000040 S H E L L " nl e c h o sp " $ B A
0000060 S H _ V E R S I O N " nl / b i n
0000100 / b a s h sp - - v e r s i o n nl
0000120 nl i f sp [ [ sp - n sp " f o o b a
0000140 r " sp ] ] ; sp t h e n nl sp sp e c
0000160 h o sp " O k " nl f i nl nl e c h o
0000200 sp " B y e " nl
0000207
In the absence of a bash
bug, it looks like something is causing the then
or fi
commands to be ignored. Perhaps there is a rogue alias that is trumping the shell built-in? What do you get with just
alias
to see all of the aliases?