Linux - General This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
05-29-2014, 02:44 AM
|
#1
|
Member
Registered: Dec 2007
Location: FL, USA
Distribution: CentOS 5.3, Ubuntu 9.04
Posts: 245
Rep:
|
how can I create a string variable in bash in a specific format in bash
I want to create a variable containing a string in the following format.
Code:
var='"pic1.jpg", "pic2.jpg", "pic3.jpg", "picN.jpg"'
so far I'm able to create pic1.jpg,pic2.jpg,pic3.jpg, but I would like to add quotation marks around each filename eg "pic1.jpg"
I'm using the following command
Code:
file=*.jpg
allPics=$(echo $file | tr ' ' ', ')
but this only takes care of the command between files, but I also need to add quotation. I also tried this approach
Code:
ls *.jpg | \
while read line; do
allPics="${allPics}, \"$line\""
done
but that doesn't seem to work.... if anyone has a better suggestion?
Thanks
Last edited by mia_tech; 05-29-2014 at 02:59 AM.
|
|
|
05-29-2014, 02:54 AM
|
#2
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,035
|
I am a little confused at what you need
Are you looking to assign a single line with all the file names quoted and separated by commas to a variable or are you looking to create an array of the file names quoted?
Option 1:
Code:
var='"pic1.jpg", "pic2.jpg", "pic3.jpg", "picN.jpg"'
Option 2:
Code:
var=('"pic1.jpg"', '"pic2.jpg"', '"pic3.jpg"', '"picN.jpg"')
There could be other alternatives depending on how you wish to retrieve the file names, ie. both of these are currently manual.
|
|
|
05-29-2014, 02:57 AM
|
#3
|
Member
Registered: Dec 2007
Location: FL, USA
Distribution: CentOS 5.3, Ubuntu 9.04
Posts: 245
Original Poster
Rep:
|
Quote:
Originally Posted by grail
I am a little confused at what you need
Are you looking to assign a single line with all the file names quoted and separated by commas to a variable or are you looking to create an array of the file names quoted?
Option 1:
Code:
var='"pic1.jpg", "pic2.jpg", "pic3.jpg", "picN.jpg"'
Option 2:
Code:
var=('"pic1.jpg"', '"pic2.jpg"', '"pic3.jpg"', '"picN.jpg"')
There could be other alternatives depending on how you wish to retrieve the file names, ie. both of these are currently manual.
|
good question... the answer is "Option 1"
I will re-edit the original post to reflect the changes
Last edited by mia_tech; 05-29-2014 at 02:59 AM.
|
|
|
05-29-2014, 03:00 AM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
One method among many others:
Code:
var=$(echo -n *.jpg | sed -r 's/^|$/"/g; s/ /", "/g')
or
Code:
var=$(echo *.jpg | awk '$1=$1{printf "\"%s\"", $0}' OFS='", "')
|
|
|
05-29-2014, 03:21 AM
|
#5
|
Member
Registered: Dec 2007
Location: FL, USA
Distribution: CentOS 5.3, Ubuntu 9.04
Posts: 245
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
One method among many others:
Code:
var=$(echo -n *.jpg | sed -r 's/^|$/"/g; s/ /", "/g')
or
Code:
var=$(echo *.jpg | awk '$1=$1{printf "\"%s\"", $0}' OFS='", "')
|
that worked great....
but I'm just wondering if it would be possible to do it with a loop like:
Code:
all=""
ls *.jpg | \
while read line; do
all="${all}, \"$line\""
done
why is the avobe example not working... is the appropiate way to concatenate string is
|
|
|
05-29-2014, 03:43 AM
|
#6
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
It is because you pass input to the loop trough a pipe: this opens a subshell where all the variables are local, so that every change made to the variable is lost when the loop terminates. Try this to see what happens:
Code:
all=""
ls *.jpg | \
while read line; do
all="${all}, \"$line\""
echo "Inside loop: $all"
done
echo "Outside loop: $all"
You can try to avoid the subshell by means of something like this:
Code:
for file in *.jpg
do
all="${all}, \"$file\""
done
but you have to adjust something to get the desired output, yet.
|
|
|
05-29-2014, 03:53 AM
|
#7
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
This one should work as desired (it uses a conditional expression to initialize the variable properly):
Code:
for file in *.jpg
do
[[ -z $all ]] && all="\"$file\"" || all="$all, \"$file\""
done
|
|
1 members found this post helpful.
|
05-29-2014, 11:34 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,035
|
Here is one a little different:
Code:
files=( *.jpg )
printf -v all "\"%s\", " ${files[@]}
echo "${files%,}"
Or maybe:
Code:
files=( *.jpg )
files=( ${files[@]/#/\"} )
files=( ${files[@]/%/\"} )
IFS=","
all="${files[*]}"
IFS=
echo "$all"
Last edited by grail; 05-29-2014 at 11:50 AM.
|
|
|
All times are GMT -5. The time now is 02:39 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|