syntax error near unexpected token `&' (using "|&")

18,106

You can use the sequence |& to pipe both stdout and stderr from one process to another.

You cannot have a space between the | and the &. (csh and tcsh allow a space; bash does not.) I suspect you happen to be typing it without the space when you run the command interactively; the syntax is the same either way.

This:

foo |& bar

is shorthand for this:

foo 2>&1 | bar

UPDATE :

With bash 3.2.25, the |& token is not recognized; was added as a new feature in bash 4.1. Running your script with the older bash, I get the same error message you do.

To make your script compatible with older versions of bash, just do the equivalent redirection without using the |& operator:

#!/bin/bash
var1=`(/usr/bin/time cdifonline -CD 186821 -ALL > /dev/null) 2>&1 | grep real | awk '{print $2}'`

Further refinements: Use $(...) rather than `...`:

#!/bin/bash
var1=$((/usr/bin/time cdifonline -CD 186821 -ALL > /dev/null) 2>&1 | grep real | awk '{print $2}')

You can also incorporate the grep search into the awk command:

#!/bin/bash
var1=$((/usr/bin/time cdifonline -CD 186821 -ALL > /dev/null) 2>&1 | awk '/real/ {print $2}')

Warning: I have not thoroughly tested these beyond verifying that they run without syntax errors.

I still see no difference between |& and | &. Is it possible that /bin/bash is a different version than what you're running interactively? Try /bin/bash --version and echo $BASH_VERSION.

Share:
18,106

Related videos on Youtube

klerk
Author by

klerk

Updated on September 19, 2022

Comments

  • klerk
    klerk over 1 year

    I have syntax error near unexpected token & in next : in bash scripts its'go like this :

    #!/bin/bash
    var1=`(/usr/bin/time cdifonline -CD 186821 -ALL > /dev/null)|& grep real|awk '{print $2}'`
    

    when i issue this command on cli i get good output, problem is when invoke this in script i can get any output from var1

    ./check_cdifonline.sh: command substitution: line 2: syntax error near unexpected token `&'
    ./check_cdifonline.sh: command substitution: line 2: `(/usr/bin/time cdifonline -CD 186821 -ALL >/dev/null) | & grep real | awk '{print $2}''
    
  • klerk
    klerk over 10 years
    (/usr/bin/time cdifonline -CD 186821 -ALL > /dev/null | awk '/real/ {print $2}') Ambiguous output redirect.
  • klerk
    klerk over 10 years
    i have same problem without spacing, command works just fine in cli and it's realy anoying
  • Jotne
    Jotne over 10 years
    Does this alone give an output? /usr/bin/time cdifonline -CD 186821 -ALL >/dev/null and if so, what?
  • klerk
    klerk over 10 years
    /usr/bin/time cdifonline -CD 186821 -ALL >/dev/null real 3.2 user 0.4 sys 0.1
  • klerk
    klerk over 10 years
    (/usr/bin/time cdifonline -CD 186821 -ALL > /dev/null)|& grep real|awk '{print $2}' 1.3 when issue on cli i get what i want i just need this in bash script to set as variable
  • Keith Thompson
    Keith Thompson over 10 years
    I just copy-and-pasted the code from your question and ran it on my own system. There was no error. (I don't have the cdifonline command, but that shouldn't affect any syntax errors.) I do get an error if I invoke it as sh ./foo.bash; how exactly are you running the script? Try copy-and-pasting the script from your question into a new file and running that.
  • klerk
    klerk over 10 years
    no it's helples, as i sad everything is just fine on cli but when invoke in script i get this error
  • Keith Thompson
    Keith Thompson over 10 years
    Did you try what I suggested? What version of bash are you using (bash --version)?
  • klerk
    klerk over 10 years
    bash --version GNU bash, version 3.00.0(2)-release (sparc-sun-solaris2.6) Copyright (C) 2004 Free Software Foundation, Inc.
  • Keith Thompson
    Keith Thompson over 10 years
    And did you try exactly what I suggested (copy-and-paste the code from your question into a file and execute that file)?