[SOLVED] assign the stdout of a command to a variable
Linux - NewbieThis 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
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.
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.
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'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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.