How to execute a bash command stored as a string with quotes and asterisk
Have you tried:
eval $cmd
For the follow-on question of how to escape *
since it has special meaning when it's naked or in double quoted strings: use single quotes.
MYSQL='mysql AMORE -u username -ppassword -h localhost -e'
QUERY="SELECT "'*'" FROM amoreconfig" ;# <-- "double"'single'"double"
eval $MYSQL "'$QUERY'"
Bonus: It also reads nice: eval mysql query ;-)
Run a string as a command within a Bash script
You can use eval
to execute a string:
eval $illcommando
Execute command as a string in Bash
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.
Bash execute string as command without expanding escaped spaces
Us an array instead of a string:
#!/usr/bin/env bash
ARGSTR=('./executable' 'test file.txt' 'arg2=true')
exec "${ARGSTR[@]}"
See:
BashFAQ-50 - I'm trying to put a command in a variable, but the complex cases always fail.
https://stackoverflow.com/a/44055875/7939871
How do I execute a command string in BASH?
Did you try eval "$CMDEXEC" >> "$LOGFILE"
?
Bash: Executing a complex command in a string, capturing output and return code
As I was painstakingly going through typing up all this question, I finally thought of a working solution, which in hindsight is rather simple:
OUTPUT=$(eval "$CMD")
EXITCODE=$?
Since I've already typed all this out and was unable to google for this myself, I figured I'd just post this and let it be.
How can we execute a string command and then get the result of it to assign a variable in shell script bash
The solution is:
strCode=$(scontrol show jodid --dd $VALUE)
echo "${strCode}"
Execute command from string variable in unix shell script
It's better to store commands in functions rather than in variables. Functions are great at it. Variables, not at all.
string() {
ps -e | less
}
string
You could use eval
to execute a command that has redirections (>
, <
) and pipes (|
), but I strongly discourage you from doing so.
eval "$string" # please don't
Bash: execute a multi-command line string in a script
the point of your question is to execute command stored in string. there are thousands of ways to execute that indirectly. but eventually, bash has to involve.
so why not explicitly invoke bash to do the job?
bash -c "$commandLine"
from doc:
-c string
If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.
http://linux.die.net/man/1/bash
Related Topics
Caputre Opengl Window in X11 with Fast Framerate - Possible
How to Fetch The Logical Name of a Nic Card Given The Ip Address Associated with It
How Does Os Chooses The Next Process to Be Run in Cpu
Building /Lib/Modules/$(Uname -R)/Build While Compiling a Kernel
Linux - Mapping User Space Memory in Kernel Code
How to Rename a Bunch of Files to Eliminate Quote Marks
Reading Memory Pointed by Register with Gdb
How to Remove File with Special Characters
Elf File Tls and Load Program Sections
How to Use Opengl Without a Window Manager in Linux
Combine Two Audio Files with a Command Line Tool
Access Podman Rest API from Testcontainer