LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-27-2012, 02:44 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Rep: Reputation: 33
[Script] How to catch the output of a command to a variable?


Hi,

In my BASH-script I want to catch the output of whoami to a variable. How to do that?

Thomas
 
Old 07-27-2012, 02:52 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Code:
variable="$(whoami)"
The $( ... ) part is what you are looking for. It executes the command (whoami in this case) in a sub-shell. Output is stored in variable.
 
Old 07-27-2012, 02:57 AM   #3
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by druuna View Post
Code:
variable="$(whoami)"
The $( ... ) part is what you are looking for. It executes the command (whoami in this case) in a sub-shell. Output is stored in variable.
Hi,

At the moment you answer my question I also found another solution:

Code:
user=`whoami`
Which one is better?
 
Old 07-27-2012, 03:01 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
People used to use `<command>`, nowadays $(<command>) is preferred.

Have a look here: http://mywiki.wooledge.org/BashFAQ/082
 
1 members found this post helpful.
Old 07-27-2012, 11:47 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
bash already has a preset $USER variable with the current user's name in it, by the way.

The bash man page section for "Shell Variables" lists several others that store commonly used environment parameters.
 
1 members found this post helpful.
Old 07-30-2012, 12:56 AM   #6
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by David the H. View Post
bash already has a preset $USER variable with the current user's name in it, by the way.

The bash man page section for "Shell Variables" lists several others that store commonly used environment parameters.
This is even a better way.
 
  


Reply


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] script output name from a variable darkego Linux - Newbie 4 07-10-2012 10:12 AM
shell script: saving output from command into a variable grcunning Linux - Newbie 7 01-26-2010 11:43 AM
csh Shell Script: Getting wc command output to variable to do math on it, how? vxc69 Programming 5 05-04-2009 04:31 PM
Script: put the output of a command into a variable. poincare999 Programming 4 11-04-2007 11:53 AM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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