Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 02-16-2009, 11:53 AM   #1
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:

echo var

I'm getting

That's ok

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

It shows

But I want it to be shown as

How can I solve it?
Thanks ...
Old 02-16-2009, 11:59 AM   #2
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
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
You can try printf in place of echo:
printf "file=>%s\n" $var
Old 02-16-2009, 12:27 PM   #4
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
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
First thing:

Originally Posted by Ejdaha View Post
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.

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:

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.

echo -n "file=>"; echo $var

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

Original Poster
Rep: Reputation: 15
Thanks for response

I solved it using

file=>'$(printf $var)'



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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:45 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration