r/shell • • 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.log
AUTORESTIC_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?

0 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/geirha Feb 10 '24
$(which autorestic) -c ...

Using which there serves no purpose; the shell already finds the command through PATH, it doesn't need a third-party tool for that. Just do

autorestic -c ...

Also, 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