LinuxQuestions.org
Visit Jeremy's Blog.
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, 03: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, 04:05 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,614

Rep: Reputation: 703Reputation: 703Reputation: 703Reputation: 703Reputation: 703Reputation: 703Reputation: 703
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, 04: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, 05:55 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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, 07:08 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

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


Reply

Tags
bash, sh


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 12:43 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration