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/geirha Feb 10 '24
Using
whichthere serves no purpose; the shell already finds the command through PATH, it doesn't need a third-party tool for that. Just doAlso, avoid using uppercase variable names for internal purposes. You risk overriding special shell variables and environment variables. You should also learn to quote properly. I recommend reading through https://mywiki.wooledge.org/Arguments