Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

linux - Bash script process substitution Syntax error: "(" unexpected

I want to run this script:

#!/bin/bash
echo <(true)

I run it as:

sh file.sh

And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this. I'm a beginner at shell scripting , but as I understand:

  1. the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
  2. I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?

Maybe it's something simple, but I couldn't find an explanation or solution.

question from:https://stackoverflow.com/questions/65848440/syntax-error-near-unexpected-token-bash

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.

See also: role of shebang at unix.SE


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...