LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-24-2010, 01:03 PM   #1
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Rep: Reputation: 34
Question Bash - brace expansion using variable


I'm trying to use brace expansion on a string, but I can't seem to get it working with variable substitution:

Code:
$ cat script.sh
#!/bin/bash

echo {1,2,3} # 1.

VAR=1,2,3
echo {$VAR} # 2.
Code:
$ sh script.sh
1 2 3
{1,2,3}
I've searched google, tldp.org and these forums and I can't seem to find an example of how this would work. I want #2 to work like #1. Any suggestions?

**Edit: I forgot to add, the values in VAR are not sequential and can have multiple digits. For example, VAR=13,17,10.

**Edit2: http://tldp.org/LDP/abs/html/bashver3.html#BRACEEXPREF3
The bottom line of code in the first gray block of code shows that you cannot use variables in such an expansion:

Code:
#!/bin/bash
a=1
b=5
echo {$a..$b}
But I'm looking for pasting the complete contents of a variable into these braces, not some portion of it...

Thanks,
Alex

Last edited by student04; 03-24-2010 at 01:15 PM.
 
Old 03-24-2010, 01:42 PM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Try using the bash builtin 'eval' to do a double expansion on the expression.
It needs to be used carefully as it can sometimes expand special characters which you don't want expanding.
Code:
VAR=1,2,3
eval echo {$VAR}
1 2 3

a=1
b=5
eval echo {$a..$b}
1 2 3 4 5
 
Old 03-24-2010, 02:26 PM   #3
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Original Poster
Rep: Reputation: 34
Thumbs up

Actually, I had considered 'eval' but wasn't sure actually how to make that work... Thanks I have a bunch of files in a directory that I needed listed and stored in a variable, but want the pattern to be separate (code is more legible):

Code:
$ cat script.sh
PATTERN=12,14,27
FILES=$(eval ls filename_{$PATTERN})
echo $FILES

$ sh script.sh
filename_12 filename_14 filename_27
 
Old 03-07-2017, 05:04 AM   #4
hilou
Member
 
Registered: May 2013
Posts: 93

Rep: Reputation: Disabled
Quote:
Originally Posted by student04 View Post
Actually, I had considered 'eval' but wasn't sure actually how to make that work... Thanks I have a bunch of files in a directory that I needed listed and stored in a variable, but want the pattern to be separate (code is more legible):

Code:
$ cat script.sh
PATTERN=12,14,27
FILES=$(eval ls filename_{$PATTERN})
echo $FILES

$ sh script.sh
filename_12 filename_14 filename_27
How to solve this one?

list=1-1.{1..9}

for i in $list;do ... done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Brace expansion: does this kind exist? exscape Linux - Software 4 04-28-2009 03:45 AM
Avoiding Shell Script Brace Expansion Woodsman Slackware 4 05-31-2008 09:36 AM
variable expansion in bash coolhandluke1 Programming 4 01-09-2008 03:45 PM
Variable expansion in BASH champak Programming 5 11-26-2007 02:44 AM
bash: extra stuff in brace expansion jhwilliams Programming 4 09-07-2007 02:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration