LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 11-10-2004, 11:35 AM   #1
BabaKali
LQ Newbie
 
Registered: Nov 2004
Posts: 9

Rep: Reputation: 0
How to echo results of variable



Hi gurus,

I want to be able to echo not just the command string that I place in a variable, but the actual results of that command.
For example, i want the statement to return a list of files:

myvar="ls -a"
echo $myvar

but it returns litteral string ls -a

Also, I want to place this in a for do loop, like this:

for F in /home/me
do
echo $myvar
done


I want to print all files in /home/me directory, don't know how.

Thanks
 
Old 11-10-2004, 11:54 AM   #2
PBSchmidt
Member
 
Registered: Aug 2004
Location: Aachen, Germany
Distribution: Debian Sarge
Posts: 129

Rep: Reputation: 15
Why don't you simply use
some@prompt:~$ $myvar (return)? this issues the command in the env-var you set up...

My EUR 0.02
 
Old 11-10-2004, 01:04 PM   #3
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
myvar="ls -a" means put the string ls-a in mycar.
myvar=`ls -a` means execute ls -a and put the results in myvar.

i'm not sure what u mean.

if you do:

for myvar in `ls -a /home/me`
do
echo $myvar
done

this will print all file contained in /home/me, one file per line.

if you do:

for myvar in `ls -a /home/me`
do
echo -n "$myvar "
done
echo

this will print all the files in one line.
 
Old 11-10-2004, 01:06 PM   #4
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
You need to tell bash you want the ls -a executed, when it's assigned to a variable like this it's just a string of characters.

One way is:-
Code:
myvar=$(ls -a)
See man bash for more...
 
Old 11-10-2004, 01:09 PM   #5
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Sorry, forgot you wanted to list the files :-)
Code:
for i in $(ls -a); do echo $i; done
 
Old 11-10-2004, 01:12 PM   #6
BabaKali
LQ Newbie
 
Registered: Nov 2004
Posts: 9

Original Poster
Rep: Reputation: 0
perfect_circle,

do you mean single-quotes in `ls -a` ?? Because my bash also returns ls -a litteral, regardless wether I type:

myvar="ls -a"
or
myvar=`ls -a`

$myvar (return) is not recognised in my bash shell (i'm using openna 1.0)

Thanks
 
Old 11-10-2004, 01:43 PM   #7
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
I mean the button under Esc.

Are you using Bash?

create a new file(lets say a.sh), and type

#! /bin/bash

myvar=`ls -a`
echo $myvar

then save the file.

then type:
bash a.sh
 
Old 11-10-2004, 01:47 PM   #8
BabaKali
LQ Newbie
 
Registered: Nov 2004
Posts: 9

Original Poster
Rep: Reputation: 0
thanks for all your help
 
  


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
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 05:38 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 03:59 PM
calling a variable within a variable sdandeker Programming 9 04-28-2004 04:55 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 04:19 AM
Echo /devPrinting doesn't work, echo /usb/lp0 works, Testpage works, Printing doesn't Hegemon Linux - General 3 08-15-2002 02:13 PM


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