LinuxQuestions.org
Help answer threads with 0 replies.
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 02-16-2009, 11:53 AM   #1
Ejdaha
Member
 
Registered: Jan 2007
Posts: 39

Rep: Reputation: 15
echo variable problem


Hi all
I have a question related with variables. Let's assume that I have one .txt file in my /tmp directory. Look here:

#!/bin/sh
var='/tmp/*.txt'
echo var

I'm getting
/tmp/file.txt

That's ok

Now I want the file name to be displayed in this sitaution to :
echo "file=>$var"

It shows
file=>/tmp/*.txt

But I want it to be shown as
file=>/tmp/file.txt

How can I solve it?
Thanks ...
 
Old 02-16-2009, 11:59 AM   #2
digerati1338
Member
 
Registered: May 2007
Location: CO
Distribution: Ubuntu
Posts: 152
Blog Entries: 1

Rep: Reputation: 18
Throw an ls in there. You want something like ls *.txt. I'll let you figure out the rest.
 
Old 02-16-2009, 12:15 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can try printf in place of echo:
Code:
printf "file=>%s\n" $var
 
Old 02-16-2009, 12:27 PM   #4
Ejdaha
Member
 
Registered: Jan 2007
Posts: 39

Original Poster
Rep: Reputation: 15
Thank you colucix for reply
I can't put it afte <<EOF

It doesn't understand it
I have to put it between <<EOF EOF

When I put $var, it understands it as /tmp/*.txt
When I put printf or echo, it doesn't understand it as LINUX command. Who to be??

Thanks ..
PLease show me an example which you 'll put it inside <<EOF EOF

Thanks ..
 
Old 02-16-2009, 12:35 PM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
First thing:

Quote:
Originally Posted by Ejdaha View Post
#!/bin/sh
var='/tmp/*.txt'
echo var
You probably mean "echo $var", and not "echo var", which would output the string "var" textually, and not the contents of the $var variable.

Quote:
Now I want the file name to be displayed in this sitaution to :
echo "file=>$var"
When trying to expand the wild card with the string "file=>" in front of $var it won't find matches, since it becomes part of the path breaking any meaning it might have. A way to proceed would be to use ls as someone else said, but beware that you need to use the proper syntax to avoid color codes which could screw your script at any given moment. You probably will also have problems with spaces in the middle of the names, depending on what are you doing.

There are many better ways to overcome this, for example, you can add the file=> string at a later stage, after the wildcard expansion has taken place, like this:

Code:
var=/tmp/*.txt
for i in $var; do echo "file=>$i"; done
EDITED: By the way, if you don't mind having all the file names on a single line you could as well use separate echos.

Code:
var=/tmp/*.txt
echo -n "file=>"; echo $var

Last edited by i92guboj; 02-16-2009 at 12:42 PM.
 
Old 02-16-2009, 12:42 PM   #6
Ejdaha
Member
 
Registered: Jan 2007
Posts: 39

Original Poster
Rep: Reputation: 15
Thanks for response

I solved it using
<<EOF

file=>'$(printf $var)'

EOF
 
  


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: instad of echo-ing, I just want to assing to a bash variable... how?? rylan76 Linux - Newbie 9 11-28-2008 09:46 AM
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 09:25 AM
How to echo results of variable BabaKali Linux - Newbie 7 11-10-2004 01:47 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 03:59 PM
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 09:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration