LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 05-29-2014, 02:44 AM   #1
mia_tech
Member
 
Registered: Dec 2007
Location: FL, USA
Distribution: CentOS 5.3, Ubuntu 9.04
Posts: 245

Rep: Reputation: 16
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.
 
Old 05-29-2014, 02:54 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 05-29-2014, 02:57 AM   #3
mia_tech
Member
 
Registered: Dec 2007
Location: FL, USA
Distribution: CentOS 5.3, Ubuntu 9.04
Posts: 245

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by grail View Post
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.
 
Old 05-29-2014, 03:00 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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='", "')
 
Old 05-29-2014, 03:21 AM   #5
mia_tech
Member
 
Registered: Dec 2007
Location: FL, USA
Distribution: CentOS 5.3, Ubuntu 9.04
Posts: 245

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by colucix View Post
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
Code:
all="${all}$line"
 
Old 05-29-2014, 03:43 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 05-29-2014, 03:53 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
Old 05-29-2014, 11:34 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH - resolve string with value of variable name leeclements Programming 9 01-12-2012 08:59 AM
[SOLVED] Bash concatenating string to variable abercrombieande Programming 4 01-19-2011 07:04 AM
[SOLVED] BASH: if Variable -eq String not working worm5252 Programming 2 01-24-2010 03:07 PM
extract string from file to variable [BASH] NikosNL Programming 13 05-07-2008 09:43 AM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 10:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:53 AM.

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