1

I've tried different suggestions on this website and elsewhere but nothing has worked. I need to pass some arguments to a shell script and then concatenate them on a string to then launch it as a command. So I do this

command="perl perl_script.pl"

for arg
do

command+="$arg "

done

eval $command

But I get this error

bowtie_script_simple.sh: 43: bowtie_script_simple.sh: comando+=-n : not found
bowtie_script_simple.sh: 43: bowtie_script_simple.sh: comando+=3 : not found

According to other threads I've seen that should work. Any ideas?

Thanks

3
  • Interesting result because the example code given shows "command" the error shows "comando+=-n" which is not the same.... are you sure this is the exact code that can cause the error? Commented Nov 29, 2012 at 15:23
  • Oh yeah sorry I wrote it in english here but it's in spanish on my code. But the code is the same, it gives the error Commented Nov 29, 2012 at 15:25
  • try command="perl perl_script.pl " Commented Nov 29, 2012 at 15:30

3 Answers 3

2
command="$command $arg"

Do your args have (or are likely to have) whitespace ? You should probably quote further for safety's sake.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah well some arguments go like this -n 3 but I figured shell will read them as separate arguments.
2

Concatenating as a string introduces difficulties when handling args with spaces. With bash/zsh/ksh, using an array works better:

command=(perl perl_script.pl)
for arg; do
    command+=("$arg")
done

# or more simply
# command=(perl perl_script.pl "$@")

# now, execute the command with each arg properly quoted. 
"${command[@]}"

From you error message, it looks like you're using /bin/sh -- that shell does not have a var+=string construct -- have to use a shell with more features.

Comments

1

I think this should work:

command="perl perl_script.pl"

for arg in $@
do
  command="$command $arg"
done

$command

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.