M
M
myppomeu2016-01-26 20:13:48
linux
myppomeu, 2016-01-26 20:13:48

How to correctly substitute the value of a variable as a command key (space handling)?

Script snippet:

WHATTOBACK="-P Music -P 'Наши документы' -P Работа/Неактуальное"
dar -c ${BACKDIR}`date -I`_"$SERIALNUMBER"_1full $WHATTOBACK

When the script is executed, the "Our Documents" folder is copied, although it should not be copied with the -P switch.
If
WHATTOBACK="-g Server/"
so everything works great.
echo $WHATTOBACK
returns the correct contents of the variable (everything inside the quotes).
Also tried the following ways:
WHATTOBACK='-P Music -P "Наши документы" -P Работа/Неактуальное'
WHATTOBACK='-P Music -P \"Наши документы\" -P Работа/Неактуальное'
WHATTOBACK='-P Music -P "Наши\ документы" -P Работа/Неактуальное'
WHATTOBACK='-P Music -P Наши\ документы -P Работа/Неактуальное'
WHATTOBACK="-P Music -P 'Наши\ документы' -P Работа/Неактуальное"

In the line with dar, I tried to replace $WHATTOBACK with `echo $WHATTOBACK`
If the same thing is run not by a script, but from the command line, then everything works as intended (i.e. the folder is not included in the archive).
How can I correctly handle this space and substitute the value of the variable as multiple keys for dar?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2016-01-27
@myppomeu

How can I correctly handle this space and substitute the value of the variable as multiple keys for dar?

WHATTOBACK=( -P Music -P 'Наши документы' -P Работа/Неактуальное )
dar -c ${BACKDIR}`date -I`_"$SERIALNUMBER"_1full "${WHATTOBACK[@]}"

To understand why nothing worked and how an array fixes everything, meditate on the Word Splitting and Arrays sections of man bash .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question