You are not logged in.
Hello all.
I am trying to make a backup script.
I am assigning folder paths to variables in a script, and I'd like
all variables would be expanded to their values.
Maybe an example will help to explain
DIR1=/home/user/Docs
DIR2=/home/user/Vids
DIR3=/home/user/Music
tar --create --xz --file=/home/Backup.tar.xz "${!DIR@}"
According to the manual:
" ${!prefix@}
Expands to the names of variables whose names begin with prefix..."
So only expanding to the names, not the values of my variables will not help.
Last edited by mountaineerbr (2019-06-04 01:59:24)
Offline
┌─[Veles ~]
└─╼ array=(/home/user/data /home/user/music /home/user/pr0n)
┌─[Veles ~]
└─╼ printf "%s\n" "${array[@]}"
/home/user/data
/home/user/music
/home/user/pr0n
Offline
Thank you, anarchist...
I will use arrays.
Offline
You ask about 'sh' but reference part of the BASH manual. If you are using BASH, the above mentioned array would likely be best, but not really necessary, just define a variable as a list:
DIRS="
/home/user/Docs
/home/user/Vids
/home/user/Music
"
tar --create --xz --file=/home/Backup.tar.xz $DIRS
Also note that in BASH, the DIR prefix will likely also match other things in the environment that you would not intend (e.g., DIRSTACK).
If you are in fact using 'sh' and not BASH, then you'd have to use a list, or the argv array:
set -- \
/home/user/Docs \
/home/user/Vids \
/home/user/Music
tar --create --xz --file=/home/Backup.tar.xz "$@"
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Hey Trilby..
Thanks for pointing to the fact it is possible to define a variable as a list..
It never crossed my mind that that would actually work!
About the question title, SH because I could not type all the question length
so I had to try and abbreviate something.. But I will change it again to state
it is a bash question, but the question title will still be incomplete.
Learning bash was indeed very productive for me today..
Offline