LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-10-2016, 08:39 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
assign the stdout of a command to a variable


I'd like to have a variables $USERS that contains all users, displayed in a row.
If I write echo `cut -d ":" -f1 /etc/passwd`, it's all fine. The users are displayed as I want them to. But how can I assign the result to the variable?

doing simply USERS=echo `cut -d ":" -f1 /etc/passwd` would include the word "echo" too. I'd prefer doing it using only one command.
 
Old 06-10-2016, 09:49 AM   #2
dab1414
Member
 
Registered: May 2011
Location: OK, USA
Distribution: Slackware 14.1_64
Posts: 76

Rep: Reputation: 52
Quote:
Originally Posted by vincix View Post
I'd like to have a variables $USERS that contains all users, displayed in a row.
If I write echo `cut -d ":" -f1 /etc/passwd`, it's all fine. The users are displayed as I want them to. But how can I assign the result to the variable?

doing simply USERS=echo `cut -d ":" -f1 /etc/passwd` would include the word "echo" too. I'd prefer doing it using only one command.
I could be wrong, no bash expert here. But, I dont think you should use echo there, if you do put it in quotes as well. Did you try this below?
Code:
USERS=`cut -d ":" -f1 /etc/passwd`
but it is good practice to avoid all uppercase names for user defined variables.
 
Old 06-10-2016, 09:51 AM   #3
Tonus
Senior Member
 
Registered: Jan 2007
Location: Paris, France
Distribution: Slackware-15.0
Posts: 1,405
Blog Entries: 3

Rep: Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514
assign the stdout of a command to a variable

VAR=$(command with args)
 
Old 06-10-2016, 10:58 AM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
USERS=$(cut -d ":" -f1 /etc/passwd
echo "$USERS"
Reference: http://www.tldp.org/LDP/abs/html/commandsub.html
http://www.tldp.org/LDP/abs/html/varassignment.html

Last edited by Habitual; 06-10-2016 at 11:02 AM.
 
Old 06-11-2016, 02:47 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Code:
users="$(cut -d: -f1 /etc/passwd)"
echo $users
 
Old 06-11-2016, 09:01 AM   #6
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Thanks for the answers. It was obvious a variable wouldn't contain a column, but a row, so I needn't have bothered myself with echo in the first place.
 
Old 06-11-2016, 09:35 AM   #7
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
Please mark as SOLVED once you have an answer
 
  


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
[SOLVED] Assign a command to a variable lamont0 Linux - Newbie 6 03-07-2016 05:58 AM
[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] How to assign value from a egrep command to variable on bash and manipulate it? karish Linux - Newbie 6 01-30-2013 04:32 PM
Assign Command value to a variable rabir Linux - Newbie 10 05-21-2012 09:01 AM

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

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