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

2

u/aioeu Feb 09 '24 edited Feb 09 '24

so basically, I can't do this the way I wanted to do it!

Not really.

You could use eval to "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'll just have to provide full cmd statements instead of trying to use these expansions.

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:

log_to() {
    local filename=$1
    shift
    "$@" >>"$filename"
}

and then you can just make your command:

log_to "$log_filename" autorestic ...

or:

autorestic ...

as required. You could even do that in one command:

# Log to $log_filename only if it is set and not empty
${log_filename:+log_to "$log_filename"} autorestic ...

(log_to is just redirecting standard output here. Your original code had 2>&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.)

1

u/thecaptain78 Feb 10 '24 edited Feb 10 '24

This is how I have it in the script:

#BACKUP
echo -e "$(datetime)\tBACKUP" | tee -a $LOGFILE
if [ $INTERACTIVE == 1 ]; then
$(which autorestic) -c $AUTORESTIC_CONFIG backup -a -v 2>&1 |& sed "s/^/\t/" | tee -a $LOGFILE
else
$(which autorestic) -c $AUTORESTIC_CONFIG backup -a --ci |& sed "s/^/\t/" 1>>${LOGFILE} 2>&1
fi

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