Hi. I'm new to bash and I'm trying to write a simple script that needs to convert a string into an array with whitespace as a delimiter.
However when i try to run the script it gives an error of "unexpected:(" or something like that. i'm away from my work station now so I'm not sure of the exact error.
My script goes something like:
#!/bin/bash
string="this is a sentence."
array=( $string )
The error occurs at the 3rd line but when I run it in terminal it doesn't give any error.
Any help would be appreciated. thanks!
scripting in linux mint 12
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
scripting in linux mint 12
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: scripting in linux mint 12
That example works fine, perhaps you should share the actual script with a problem.
Running this:
Gives the expected output:
Running this:
Code: Select all
#!/bin/bash
string="this is a sentence."
array=( $string )
for item in "${array[@]}"; do
echo $item
done
Code: Select all
this
is
a
sentence.
Re: scripting in linux mint 12
Thanks for the quick response.
This is the error that your script returns:
xtest.sh: 3: Syntax error: "(" unexpected
The error only shows when I try to run it as a script but when I input each line directly into the terminal it works as normal.
I'm using LM 12, btw. I read somewhere that LM uses dash instead of bash but I started my script with #! /bin/bash so I have no idea why it doesn't work.
edit: I chmod +x my script and now it works fine. I didn't know that there would be any difference. Thanks for the help.
This is the error that your script returns:
xtest.sh: 3: Syntax error: "(" unexpected
The error only shows when I try to run it as a script but when I input each line directly into the terminal it works as normal.
I'm using LM 12, btw. I read somewhere that LM uses dash instead of bash but I started my script with #! /bin/bash so I have no idea why it doesn't work.
edit: I chmod +x my script and now it works fine. I didn't know that there would be any difference. Thanks for the help.