[SOLVED] Can variables be used in multiple directory {f1,f2,...,N} syntax?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Can variables be used in multiple directory {f1,f2,...,N} syntax?
Can variables be used with the multiple directory {folder1,folder2,...,folder N} syntax?
I would like to check how many total files are in four specific sub-directories of the ~/.config/cairo-dock directory. I successfully find out how many by the command:
Code:
$ find ~/.config/cairo-dock/{current_theme,stack,themes,third-party} -type f | wc -l
829
I would like to use a variable to replace the {folder1,folder2,...,folder N} syntax. E.g.
Code:
DIR=current_theme,stack,themes,third-party
and then
Code:
$ find ~/.config/cairo-dock/{${DIR}} -type f | wc -l
Unfortunately this results in
Code:
find: `/home/a/.config/cairo-dock/{current_theme,stack,themes,third-party}': No such file or directory
Whenever the ${DIR} is used and not the longhand {current_theme,stack,themes,third-party} it doesn't work.
Using variables would be more convenient in a script where the {current_theme,stack,themes,third-party} is repeated.
Some attempts are below:
ATTEMPTS
I first set the variable:
Code:
DIR=current_theme,stack,themes,third-party
Next , I check that this variable will display correct text:
well if its just to find teh no of files in sub directories, i would say the approach doesn't looks good, you are using find where you already know the location of files :
A simple code for your requirement though it would not be the compact of all which can be achieved.
Code:
for i in /home/a/.config/cairo-dock/; do echo -e "$i:\c ";ls $i|wc -l; done
Last edited by SAbhi; 01-19-2016 at 03:30 AM.
Reason: re-phrase
To those of us who hold safety as a top priority, due to bash's order of expansion using variables inside brackets simply is not an efficient practice. Please peruse the answer below from http://www.linuxquestions.org/questi...rs-4175563266/.
Quote:
Originally Posted by rknichols
The relevant paragraph from the bash manpage is under EXPANSION:
The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.
Brace expansion is performed before variable expansion, so "{${ALPHA}}" isn't seen as a legitimate brace expansion (there's no comma inside those braces) and is left as-is. (The output from "-x" puts quotes around it since it contains characters that are special to the shell.)
You can force the expression to be re-parsed after variable expansion by using eval, but that is extremely dangerous unless you have absolute confidence that the string being passed to eval can't contain anything dangerous like content from an outside source, since that could include things like command substitution running arbitrary commands. If you are sure that the content of ALPHA is always safe, then
Code:
eval mkdir -p 1 {${ALPHA}}
No Sir, I'm not forcing anything!! Safety prevents mishaps. Preventive maintenance saves time in the long run.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.