Syntax error: "(" unexpected (expecting word) --- in my bash script

15,961

Solution 1

Try :

chmod 755 "my_script.sh"

Then run it simply like this

my_script.sh

The #!/bin/bash line at the beginning is used to tell your system which shell you should run the script with. I think you are overiding this by executing it with sh my_script.sh. You could also explicitly write /bin/bash my_script.sh. Also if you have some bash specific syntax in your script, you should consider changing the extension to .bash to be more explicit.


EDIT

You don't seem to have bash on your FreeBSD distro (the default shell on FreeBSD appears to be tcsh). You can find here a tutorial to install bash on FreeBSD. The solution I provided should then work properly. Best of luck.

Solution 2

Obviously from the she-bang, that script is intended to be run by bash not sh (even though the syntax looks more like zsh syntax because of the unquoted variables).

You'll want to run it with bash or zsh. If those shells are not available, you can install them or alternatively, translate that script to sh syntax which should be straightforward here.

The sh language (both Bourne or POSIX) has no <(...) operator. That comes from ksh and is supported by bash and zsh as well. echo -e is non-standard and even with bash and ksh only works in some environments.

The standard sh equivalent would be:

var1="var1"
var2="var2"
var3="var3"

cat < "$var1" || exit
printf '<something>\n' || exit
cat < "$var2/file123.txt" || exit
printf '</something>\n<something2>\n' || exit
cat < "$var3//file456.txt" || exit
print '</something2>\n'
Share:
15,961

Related videos on Youtube

Kurama
Author by

Kurama

Updated on September 18, 2022

Comments

  • Kurama
    Kurama over 1 year

    I have a bash script:

    #!/bin/bash
    
    VAR1="var1"
    VAR2="var2"
    VAR3="var3"
    
    cat ${VAR1} \
        <(echo -e '<something>') \                       # <--------- here's the error
        ${VAR2}/file123.txt \
        <(echo -e '</something>\n<something2>') \
        ${VAR3}/file456.txt \
        <(echo -e '</something2>')
    

    When I run it: sh my_script.sh, I get the error:

     my_script.sh: 9: Syntax error: "(" unexpected (expecting word)
    

    update:

    bash isn't found, "/bin/bash" doesn't exist. neither bash does.

    • maulinglawns
      maulinglawns over 7 years
      It would be helfpul if you described what you want to achieve.
    • Admin
      Admin over 7 years
      It works fine for me.
    • smw
      smw over 7 years
      How are you running the script? The error is characteristic of executing a bash script with a shell that doesn't support process substitutions like <(echo -e '<something>'). See Default Shell
    • Stéphane Chazelas
      Stéphane Chazelas over 7 years
      You're trying to run a bash script with sh. Use bash my_script.sh.
    • Kurama
      Kurama over 7 years
      @StéphaneChazelas, bash -- command not found and ls: /bin/bash: No such file or directory
    • user
      user over 7 years
      @Kurama What can you tell us about your environment? You tagged this FreeBSD; what can you tell us about the installed version, packages, ...?
    • Stéphane Chazelas
      Stéphane Chazelas over 7 years
      That syntax is bash syntax (strictly speaking it's more zsh syntax as the variables are unquoted). You'll need to either install bash or zsh, or modify it to be in the syntax supported by your sh or any other shell available on your system.
  • Kurama
    Kurama over 7 years
    my_script.sh: command not found. neither ./my_script is.
  • Akhil
    Akhil over 7 years
    Where did you get that script ? It seems that you are trying to run a bash-specific script while not having bash available on your system. You can either try to install bash or try to adapt the script so that it runs on your system's shell.
  • Akhil
    Akhil over 7 years
    I have added a link in my answer to a tutorial explaining how to install bash on FreeBSD.
  • Claus Andersen
    Claus Andersen over 7 years
    On FreeBSD we often recommend to use #!/usr/bin/env bash as the first line in the script. This allows for better cross system portability as bash is not placed in /bin.
  • skyuuka
    skyuuka almost 4 years
    I have a similar issue for a binary file compiled from C code. I cannot install bash as it is an embedded system. Is there anything I can do to make the compiled binary runable in sh?
  • Akhil
    Akhil over 3 years
    @skyuuka I'm a little late to the party, but compiled binary files don't need to be interpretted by the shell to be executed, they are instructions directly executable by your CPU. If you got the program precompiled you should try to compile it again, or follow a tutorial on how to produce an executable from C code (there are many out there). Best of luck !