syntax error near unexpected token `&' (using "|&")
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
.
Related videos on Youtube
klerk
Updated on September 19, 2022Comments
-
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 over 10 years(/usr/bin/time cdifonline -CD 186821 -ALL > /dev/null | awk '/real/ {print $2}') Ambiguous output redirect.
-
klerk over 10 yearsi have same problem without spacing, command works just fine in cli and it's realy anoying
-
Jotne over 10 yearsDoes this alone give an output?
/usr/bin/time cdifonline -CD 186821 -ALL >/dev/null
and if so, what? -
klerk over 10 years/usr/bin/time cdifonline -CD 186821 -ALL >/dev/null real 3.2 user 0.4 sys 0.1
-
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 over 10 yearsI 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 assh ./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 over 10 yearsno it's helples, as i sad everything is just fine on cli but when invoke in script i get this error
-
Keith Thompson over 10 yearsDid you try what I suggested? What version of bash are you using (
bash --version
)? -
klerk over 10 yearsbash --version GNU bash, version 3.00.0(2)-release (sparc-sun-solaris2.6) Copyright (C) 2004 Free Software Foundation, Inc.
-
Keith Thompson over 10 yearsAnd did you try exactly what I suggested (copy-and-paste the code from your question into a file and execute that file)?