2

I have 2 scripts:

/home/bin/test.sh

#!/bin/bash
. /home/bin/test_functions.sh
test

/home/bin/test_functions.sh

#!/bin/sh
test()
{
    echo "this is a test"
}

I wanted to call the function from the external script and execute it in the main script. Yet I've been receiving these errors:

'home/bin/test_functions.sh: line 2: syntax error near unexpected token `
'home/bin/test_functions.sh: line 2: `test()

What could be wrong with what I'm doing?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user123
  • 1,060
  • 3
  • 13
  • 29
  • This works for me. Something might be wrong with your file. Does each one have execute permission? – EJK Jun 18 '15 at 03:40
  • 1
    Don't use the name `test`; it is a shell built-in command (and also usually an executable found in either `/bin` or `/usr/bin`) and that will confuse you even if it doesn't confuse the shell. – Jonathan Leffler Jun 18 '15 at 03:41
  • Yes, I've already changed the permissions for both files in 755. – user123 Jun 18 '15 at 03:42
  • @JonathanLeffler i've already tried a different function name yet i still got the same errors and `./test.sh: line 3: testfunc: command not found` – user123 Jun 18 '15 at 03:43

1 Answers1

5

It appears that test_functions.sh is in DOS format and bash is choking on the \r\n line endings. Use dos2unix to convert it to UNIX line endings.

You can tell because what bash is trying to output is this:

/home/bin/test_functions.sh: line 2: syntax error near unexpected token `\r'
/home/bin/test_functions.sh: line 2: `test()\r'

But the carriage returns \r cause the single quotes to end up at the beginning of the error messages, overwriting the leading /.

Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578