Move linux command into shell script [duplicate] - bash

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:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
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.

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

Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.

sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

Related

syntax error near unexpected token `(' bash [duplicate]

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:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
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.
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
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

Shell script error - bad substitution - principle

I am new in shell scripting and all questions I found about this was so complicated that I decided to ask the question about the principle of this error.
I have a test.sh file which looks like:
var1="I love Suzi Suzi and Marry"
var2="Sara"
echo "${var1//Suzi/$var2}"
if I run it in terminal via sh test.sh I am getting this error - Bad substitution. Can somebody tell me please what is wrong with it? Thank you.
If I run it in terminal via sh test.sh
There's your problem. ${parameter/pattern/string} is bash syntax, not vanilla sh. Run it via bash test.sh (And/or put an appropriate shebang in your script and make it executable so you can just run ./test.sh).

Bash script works manually but not as cron job [duplicate]

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:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
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.
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
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh

"source" command in shell script not working [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 5 years ago.
I have a file to be sourced in Centos 7.
It just works fine if I do :
$ source set_puregev_env
however, if I put this in a shell script, it doesn't work..
$ sh xRUN
xRUN: line 3: source: set_puregev_env: file not found
this is my shell script : xRUN
#!/bin/bash
source set_puregev_env
can anyone tell me what I might be doing wrong, or missing?
source is a command implemented in bash, but not in sh.
There are multiple ways to fix your script. Choose either one.
Run the script using bash interpreter
When you are invoking the xRUN script - you are explicitly telling it to be interpreted by sh
$ sh xRUN
To change and interpret the script with bash instead do
$ bash xRUN
This will make bash interpret the source command, and your script will work.
Use dot command to make script bourne compatible
You can also change the source with a dot command which does the same thing but is supported in both bourne and bash.
Change the line:
source set_puregev_env
With:
. set_puregev_env
Now the script will work with either sh or bash.
Make script executable
You should also run the script directly to avoid confusions like these by making it executable chmod +x xRUN, and invoking it like this:
$ ./xRUN
It will then use the command specified in the shebang and use the rest of the script as input. In your case it will use bash - since that is specified in the shebang.

Error 'Syntax error: "(" unexpected' when declaring arrays in bash

Same problem as this OP, but must be a seperate cause.
The following script:
#!/bin/sh
arr=("cat" "dog" "bird")
Works interactively (debian) but fails when called by crontab with:
/bin/sh: 2: /path/zero_check.sh: Syntax error: "(" unexpected
I've tried with #!/bin/bash shebang, and declaring array with declare -a arr=("cat" "dog" "bird"), to no effect.
Any idea why?
The problem here is that you are using this shebang:
#!/bin/sh
Whereas arrays are something Bash specific that shell does not allow.
So to make it work, change the shebang of your script to Bash:
#!/bin/bash
Specify your interpreter explicitly in the crontab entry. Use
bash /path/zero_check.sh
rather than
/path/zero_check.sh
Just for the documentation,
i had an old script to run which had an Syntax Error in the Shebang:
#/bin/bash
instead of
#!/bin/bash
Also check the Script is executable of course.
Very similar problem with incorrect bash function declarations. This works OK from the command line, but it causes cron to fail...
function test () { ... }
Cron should save the errors in /var/mail
I also recommend linting with "shellcheck" because it found another error I didn't notice.

Resources