bash: appending to list fails with "not found" [duplicate] - bash

This question already has an answer here:
Syntax error in sh - built and working for bash [duplicate]
(1 answer)
Closed 5 years ago.
I have the following script
list=\(\)
val=123
list+=\(\"$val\"\)
When I copy-paste it to the terminal it works fine, but when I try to run it from inside a .sh file, I get this error:
file.sh: 9: file.sh: list+=("123"): not found
I found this answer here, but it doesn't seem applicable since I'm not using a space.
btw, I use \ in front of ( because otherwise I get Syntax error: "(" unexpected, this also happend only when running from a script file.
What causes the error?
I'm working on GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)

You are assigning the literal string () to list, not implicitly declaring it as an empty array.
list=()
val=123
list+=("$val")

Related

Bash - length of command line argument $0 [duplicate]

This question already has answers here:
How to create shell variable with dashes?
(2 answers)
Closed 1 year ago.
I want to find the length of $0
If I use the following bash code
leading-space=${#0}
I get the following result:
./Install: line 25: leading-space=9: command not found
$0 has the value ¨./Install¨
The length of the string appears to be correct, but then bash gets confused. I am running bash 5.0.17 on Ubuntu 20.10. Can anyone see what I am doing wrong?
Identifiers in bash may not contain -, therefore the whole assignment is interpreted as a command (like cd, or grep) but there is no command with the name leading-space=9 on your system, resulting in the error.
Use the following:
leading_space=${#0}

Command substitution with variables gives command not found? [duplicate]

This question already has answers here:
Getting "command not found" error in bash script
(6 answers)
Closed 3 years ago.
I'm having a BASH script:
#!/usr/bin/env bash
PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
YAML=$(envsubst < ${PATH}/test.yml)
echo "${YAML}"
It results:
./test.sh: line 5: envsubst: command not found
I see that having a variable inside command substitution causes such error.
If I'm not using any:
YAML=$(envsubst < ./test.yml)
Then I'm having an expected successful script execution.
I've tried different syntax using quotes all over the place, but nothing helped.
How do I successfully use variable inside command substitution?
Bash version:
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
UPDATE: A duplicate question. Found an answer here:
https://stackoverflow.com/a/5642584/5935309
The problem is that you are changing PATH, which is used by Bash internally to define where to look for programs (and in which order).
You changed PATH to only contain the current working dir, and that's not where envsubst is located.
The solution is to use something different than PATH, like FILE_PATH.

Assigning value not working in a shell [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 7 years ago.
OS: Ubuntu 14.04
I created a test file called test and in it I have:
#!/bin/bash
NEW_VALUE = "/home/"
echo $NEW_VALUE
chmod +x test
./test
Produces the following:
./test: line 2: NEW_VALUE: command not found
Any ideas why this is not working?
In Bash you can't have any space around the = sign when assigning a variable.
Any space will end the assignment, even after the =, e.g.:
test_var=this is bad
#=> is: command not found
#CharlesDuffy's comment explaining why this happens
Check this link for more information on variable assignment in bash: http://wiki.bash-hackers.org/scripting/newbie_traps#setting_variables
Remove the white space while assigning then it'll work fine. just like:
#!/bin/bash
NEW_VALUE="/home/"
echo $NEW_VALUE

`Syntax error: "(" unexpected` when trying to set a list of files as an array variable in bash [duplicate]

This question already has an answer here:
I got 'syntax error: "(" unexpected' (expecting "done") [duplicate]
(1 answer)
Closed 7 years ago.
Maybe I'm being really silly here but I can't seem to figure this out:
#!/usr/bin/env bash
...
DELTAS=($(ls -p /foo/bar/ | grep -P '^\d+[^\.]+\.sh$'))
...
If I run this interactively it's fine, and echo $DELTAS returns:
1-foo.sh
2-bar.sh
However, when this is run inside a bash script, I receive Syntax error: "(" unexpected; any ideas?
I'm guessing that you've not put #!/bin/bash at the top of your script, and so it's running using /bin/sh rather than /bin/bash. This would run under dash rather than bash on Ubuntu, for example.
That syntax is a bash extension.

I got 'syntax error: "(" unexpected' (expecting "done") [duplicate]

This question already has answers here:
I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"
(3 answers)
Closed 7 years ago.
I have a very simple shell script which I'm using to loop through directories, and call another shell script. I wrote it on my local machine (OS X running Bash 3.2) and am using it on a remote server running Bash 4.2.
On the server, when I type which bash, I get /bin/bash, so I added the line on top. I still get this error, pointing to the line that begins arrIN=...
8: run_all_verification.sh: Syntax error: "(" unexpected (expecting "done")
The shell script:
#!/usr/bin/bash
# Base name for all experiments
BASE_EXP_ID=$1;
for i in ${BASE_EXP_ID}*
do
# Split file name by "__"
arrIN=(${i//__/ });
EXP_ID=${arrIN[0]}
NUM_FEATURES=${arrIN[1]}
echo "${EXP_ID} ${NUM_FEATURES}"
sh run_verification.sh ${EXP_ID} ${NUM_FEATURES}
done
Your error message is from Dash, probably because you ran sh filename.
To run a script with Bash, use bash filename (or ./filename).

Resources