How to execute a command within a bash script?

I am writing a script that will continuously broadcast some data to port 30000, so a python udp listener can get the data and insert it into a table, from which I can draw that information and display on a web page.

So, I want to execute

echo -n -e "\x00\x00\x0A\x00 1 012601234512345\x00" | nc -w1 -u localhost 3000

in the bash script in a while loop.

This is the bash script I have so far:

#!/bin/bash
echo 'Running Script'   
a=0  
x=1
while [ $a -le 1 ]
do
  echo 'echo -n -e "\x00\x00\x0A\x00        1      012601234'$x'12345\x00" | nc -w1 -u localhost 30000'
  sleep 5 
  let x=$(($x + 1))
done

The x variable is incrementing each time throughout the loop so the data changes each time.

At the moment I am thinking that rather than outputing that as a command to the terminal, it is just coming out as a string. I am very new to bash, and any help would be greatly appreciated.


You're overthinking it.

Just put the command you want to execute there, without quoting it or echoing it. Replace the variable with ${x}. Variables will be substituted in double-quoted strings, and the {} is there to make sure that your variable is interpreted as x and not x12345.

#!/bin/bash
echo 'Running Script'   
a=0  
x=1
while [ $a -le 1 ]
do
  echo -n -e "\x00\x00\x0A\x00        1      012601234${x}12345\x00" | nc -w1 -u localhost 30000
  sleep 5 
  let x=$(($x + 1))
done

You are indeed echoing the command instead of executing it.

Bash scripts are executed one by line as commands. So this line:

echo 'Running Script'

Is an exact equivalent of typing echo 'Running Script' in shell. Try it yourself: it will output Running Script. Conclusion: echo will output all its arguments to the terminal.

Screenshot from bash with above command evecuted

That being said, this line:

echo 'echo -n -e "\x00\x00\x0A\x00        1      012601234'$x'12345\x00" | nc -w1 -u localhost 30000'

will output echo -n -e "\x00\x00\x0A\x00 1 012601234'$x'12345\x00" | nc -w1 -u localhost 30000 to the terminal Entire line is executed, argument it outputted.

Second command executed in bash

It still looks confusing, so let's simplify it a bit: let our command be echo 'echo -n -e "blah"'.

blah command ran in bash

You've told it to output the command, so it has outputted the command. Drop the outer echo to execute it instead. As slhck suggested, you can make use of the variable by wrapping it with curly braces:

echo -n -e "\x00\x00\x0A\x00        1      012601234${x}12345\x00" | nc -w1 -u localhost 30000

It will execute the command, but not output it. If you want to output the command, try this:

cmd="echo -n -e \"\x00\x00\x0A\x00        1      012601234${x}12345\x00\" | nc -w1 -u localhost 30000"
echo $cmd
$cmd

First line assigns your command to a cmd variable, second line outputs it and third one executes it. It looks confusing at first, but it's pretty simple: bash will just replace variables before executing a command, so typing variable name will cause its content to be executed.