Author Topic: How to save terminal printout as variable  (Read 1211 times)

Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
How to save terminal printout as variable
« on: November 04, 2011, 02:05:43 PM »
I'm trying to save the information printed out of the terminal as a variable. For example if I want to put in whoami and save it as MSG:
Code: [Select]
whoami
echo "You are currently logged in as $MSG"

I can't seem to find anything that dose work to save the impute as a variable, most things like using ret=$? returns a value of 0.

The two commands I'm trying to take the imput form are:
Code: [Select]
whoami
cat /etc/passwd | grep "/home" |cut -d: -f1

-Thank You

Offline pags

  • Hero Member
  • *****
  • Posts: 2602
  • Keep it clean.
Re: How to save terminal printout as variable
« Reply #1 on: November 04, 2011, 02:08:41 PM »
Try something like this:
Code: [Select]
[jpaglia@core2pclinux ~]$ VAR=`whoami`; echo $VAR
jpaglia
[jpaglia@core2pclinux ~]$

*Notice the "back ticks" surrounding the "whoami" command...

For your example, try:
Code: [Select]
HOMES=`cat /etc/passwd | grep "/home" |cut -d: -f1`; echo $HOMES
and
Code: [Select]
[jpaglia@core2pclinux ~]$ VAR=`whoami`; echo "You are currently logged in as "$VAR
You are currently logged in as jpaglia
[jpaglia@core2pclinux ~]$

« Last Edit: November 04, 2011, 02:16:25 PM by pags »

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: How to save terminal printout as variable
« Reply #2 on: November 04, 2011, 02:12:14 PM »
For future use / information, in place of
Code: [Select]
"/home"     
try using
Code: [Select]
$HOME     

Offline glamdring

  • Hero Member
  • *****
  • Posts: 552
Re: How to save terminal printout as variable
« Reply #3 on: November 04, 2011, 02:17:46 PM »
For future use / information, in place of
Code: [Select]
"/home"     
try using
Code: [Select]
$HOME     

That filtered out the 1 odd user being reported, thanks!

pags your solution worked perfectly, thank you a lot! I searched many bash tutorials, but couldn't seem to find this.

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15829
  • LXDE! Coffee, Bacon and Cheesecake!
Re: How to save terminal printout as variable
« Reply #4 on: November 04, 2011, 02:30:02 PM »
In bash scripts, $HOME always refers to the /home/<username> of the currently logged in user.     

Online The Chief

  • Hero Member
  • *****
  • Posts: 2337
Re: How to save terminal printout as variable
« Reply #5 on: November 05, 2011, 01:22:15 PM »

Code: [Select]
[jpaglia@core2pclinux ~]$ VAR=`whoami`; echo "You are currently logged in as "$VAR
You are currently logged in as jpaglia
[jpaglia@core2pclinux ~]$

Wouldn't/shouldn't this work as well?

Code: [Select]
[jpaglia@core2pclinux ~]$ echo "You are currently logged in as " `whoami`
You are currently logged in as jpaglia
[jpaglia@core2pclinux ~]$

Never mind - just tried it and it works fine.  As I surmised it would.  No need for a variable - unless you intend on doing something else with the data.

But note the space between the back tic and the w also prints out.





Retired Senior Chief, Retired Software Engineer, Active GrandPa