Pass command arguments as a variable


Answer: 1

28 hours ago

I have made a bash script to back up my AWS Ligthsail server with restic. Everything works finally, but there is one thing I couldn't find an answer.

Just the part where the problem is:

forget_policy=(--keep-within-daily 7d --keep-within-weekly 1m --keep-within-monthly 1y --keep-within-yearly 2y)

//(… other code)

forget_old () {
    # Forget and prune
    restic -r $RESTIC_REPOSITORY forget "${forget_policy}" --prune | log

    # Check if exit status is ok
    if [ $status -ne 0 ]; then
        log "Forget failed ${status}"
        exit 1

//(… other code)



>> invalid argument "--prune" for "--keep-within-daily" flag: no number found

I can't pass the $forget_policy variable to the forget command. When I wrap the varibale in "" I get

>> unknown flag: --keep-within-daily 7d --keep-within-weekly 1m --keep-within-monthly 1y --keep-within-yearly 2y

When I copy the variable content directly to the command, it works. So I must be doing something wrong with passing the variable.

Added by: Mr. Evans Auer IV

Answer: 2

21 hours ago

To expand each of the elements of your argument array as a separate word you need "${forget_policy[@]}"

"${forget_policy}" is equivalent to "${forget_policy[0]}" so only expands to the first argument - which is why you end up with --keep-within-daily --prune

See the Arrays subsection under PARAMETERS in the bash manual page.

Popular Search

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9