11

I'm testing a short bash script. I'd like to execute a string as a command.

#!/bin/bash

echo "AVR-GCC"
$elf=" main.elf"
$c=" $main.c"
$gcc="avr-gcc -mmcu=atmega128 -Wall -Os -o $elf$c"
eval $gcc
echo "AVR-GCC done"

I know it's ugly and all, but shouldn't it execute the avr-gcc command? The errors are the following:

./AVR.sh: line 4: = main.elf: command not found
./AVR.sh: line 5: = .c: command not found
./AVR.sh: line 6: =avr-gcc -mmcu=atmega128 -Wall -Os -o : command not found
1
  • 2
    You should not have dollar signs left of the assignment: i.e. elf=" main.elf" not elf=" main.elf Commented Nov 5, 2013 at 10:02

2 Answers 2

18

I don't know what your final goal is, but you might instead consider using the following more robust way: using arrays in bash. (I'm not going to discuss the several syntax errors you have in your script.)

Don't put your commands and its argument in a string as you did and then eval the string (btw, in your case, the eval is useless). I understand your script as (this version will not give you the errors you mentioned, compare with your version, especially there are no dollar signs for variable assignments):

#!/bin/bash

echo "AVR-GCC"
elf="main.elf"
c="main.c"
gcc="avr-gcc -mmcu=atmega128 -Wall -Os -o $elf $c"
eval $gcc
echo "AVR-GCC done"

You'll very soon run into problems when, for example, you encounter files with spaces or funny symbols (think of a file named ; rm -rf *). Instead:

#!/bin/bash

echo "AVR-GCC"
elf="main.elf"
c="main.c"
gcc="avr-gcc"
options=( "-mmcu=atmega128" "-Wall" -"Os" )
command=( "$gcc" "${options[@]}" -o "$elf" "$c" )
# execute it:
"${command[@]}"

Try to understand what's going on here (I can clarify any specific points you'll ask me to), and realize how much safer it is than putting the command in a string.

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

3 Comments

Thank you very much! :) I totally understand your solution.
"I don't know what your final goal is..." - he has a "string" variable with a command and arguments. He's trying to invoke it as a command.
Lovely solution
5

You don't use the dollar sight when creating variables, only when accessing them.

So change

$elf=" main.elf"
$c=" $main.c"
$gcc="avr-gcc -mmcu=atmega128 -Wall -Os -o $elf$c"

to

elf=" main.elf"
c=" $main.c"
gcc="avr-gcc -mmcu=atmega128 -Wall -Os -o $elf$c"

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.