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
1
u/thecaptain78 Feb 10 '24 edited Feb 10 '24
This is how I have it in the script:
#BACKUPecho -e "$(datetime)\tBACKUP" | tee -a $LOGFILEif [ $INTERACTIVE == 1 ]; then$(which autorestic) -c $AUTORESTIC_CONFIG backup -a -v 2>&1 |& sed "s/^/\t/" | tee -a $LOGFILEelse$(which autorestic) -c $AUTORESTIC_CONFIG backup -a --ci |& sed "s/^/\t/" 1>>${LOGFILE} 2>&1fi