LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-05-2014, 02:51 PM   #1
useretail
LQ Newbie
 
Registered: May 2014
Posts: 6

Rep: Reputation: Disabled
Question [BASH] Assign output to variable(s)


Let's say I have a script that outputs a string with multiple values divided by '?':
Quote:
# ./test
aaa?bbb?ccc
Is it possible to assign aaa, bbb, ccc to three variables in one query (one line command)?
Can you do it using only bash?
Can you do it without sed/perl/awk?

Like this:
Quote:
# ./test | somecommand
# echo $var1
aaa
# echo $var2
bbb
# echo $var3
ccc
 
Old 12-05-2014, 03:05 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Not that I know of, since some bash substitutions can't be nested. It's easy enough to do in two lines:

Code:
Q=$(./test)
read var1 var2 var3 <<<${Q//\?/ }
 
1 members found this post helpful.
Old 12-05-2014, 03:14 PM   #3
useretail
LQ Newbie
 
Registered: May 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks, smallpond. Why there are three redirections?
 
Old 12-05-2014, 04:55 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
From the bash manpage:
Code:
 Here Strings
       A variant of here documents, the format is:

              <<<word

       The word is expanded and supplied to the command on its standard input.
In the example provided, the data is passed through bash string expression evaluation to then be "read" and assigned to the given variables.
 
1 members found this post helpful.
Old 12-06-2014, 06:08 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Or you could use IFS;
Code:
IFS=? read var1 var2 var3 <<<$(./test)
 
1 members found this post helpful.
  


Reply

Tags
bash, sh



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
[SOLVED] Bash script: How to assign variable to an sqlite3 command with variable embedded? ninja6o4 Linux - Software 10 02-15-2015 04:43 PM
[SOLVED] BASH: Assign a variable to a variable name... c_henry Programming 9 06-04-2012 11:33 AM
Can not assign variable to output of sed protocol Linux - General 5 10-24-2011 04:36 PM
How to assign a variable to the output value of a program BeyondSora Linux - Newbie 3 02-07-2011 12:46 AM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 06:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:06 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