r/shell • u/thecaptain78 • Feb 09 '24
Expanding variable as command line parameters causes single quotes round strings
I am trying to place the string contents of a shell variable into a command as arguments. When bash expands the variable it places single quotes around elements of the string.
#!/bin/bash
LOGFILE="/var/log/autprestic.logAUTORESTIC_CONFIG="/home/xxxx/.autorestic.yml"RESTIC_PARAMS="--ci 2>&1 >> $LOGFILE"
$(which autorestic) -c $AUTORESTIC_CONFIG backup -a ${RESTIC_PARAMS}
Results in:
/usr/local/bin/autorestic -c /home/xxxx/.autorestic.yml backup -a --ci '2>&1' '>>' /var/log/autorestic.log
Why do the expanded parameters have single quotes around them?
View all comments
Show parent comment
2
u/aioeu Feb 09 '24 edited Feb 09 '24
Not really.
You could use
evalto "take a string, then interpret that string as an entire command"... but I really don't recommend it. Find some other way to do what you want to do.I bet there's a way to do it that doesn't involve lots of "full cmd statements". For instance, let's say you want to make "redirecting to a log file" optional. You could have a function:
and then you can just make your command:
or:
as required. You could even do that in one command:
(
log_tois just redirecting standard output here. Your original code had2>&1 >>"$LOGFILE", which doesn't entirely make sense. If you want to log both standard output and standard error to the file, you would need to do those redirections the other way around.)