S
S
Sergey Karbivnichy2021-11-23 17:40:17
linux
Sergey Karbivnichy, 2021-11-23 17:40:17

How to "unpack" parameters from a variable in a bash script?

There is a simple script to display the size of the home directory, excluding some directories:

du -sh ~ --exclude=$HOME/{.cache,.config/google-chrome,Загрузки}

That's how it works. But if the exclusion directories are added to the variable (for convenient editing of the list in the future), then it does not work:
excludes=".cache,.config/google-chrome,Загрузки"
du -sh ~ --exclude=$HOME/{$excludes}

I've searched a lot of sites but haven't found a solution yet. I also tried to change the quotes, remove them - it did not bring any result.
Here is what the bash debugger shows:
[email protected]:~$ bash -x ./t.sh 
+ excludes=.cache,.config/google-chrome,Загрузки
+ du -sh /home/sergey '--exclude=/home/sergey/{.cache,.config/google-chrome,Загрузки}'
2,2G	/home/sergey

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xibir, 2021-11-23
@hottabxp

In your case it will work like this:

excludes=".cache,.config/google-chrome,Загрузки"
eval du -sh ~ --exclude=$HOME/{$excludes}

V
Victor Taran, 2021-11-23
@shambler81

ncdu look, it's like htop only for du
ncdu.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question