The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Difference between using "echo" builtin and /bin/echo ulidtko Shell Programming and Scripting 2 07-15-2009 12:12 PM
what does echo $$, $? $# mean gokulagiridaran Shell Programming and Scripting 8 08-21-2008 01:22 PM
echo $0 mainegate Shell Programming and Scripting 2 09-24-2007 02:34 AM
echo not echoing correctly shorty UNIX for Dummies Questions & Answers 3 09-25-2006 05:45 PM
How to set echo on siegfried Shell Programming and Scripting 1 11-18-2005 01:56 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-10-2009
mokachoka mokachoka is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 23
How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example...

i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos...

echo "echo "hello"" >$file
echo "echo "goodbye"" >$file

Why is this not correct?

Thanks.
  #2 (permalink)  
Old 09-10-2009
sharadpisal sharadpisal is offline
Registered User
  
 

Join Date: May 2008
Posts: 28
it should be echo "echo \" somthing\" " > $file
  #3 (permalink)  
Old 09-10-2009
siba.s.nayak siba.s.nayak is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 166
echo "echo "hello"" >$file

If you want only hello should be written to $file, then
The command should be
Code:
echo `echo "hello"`

For mor ecample:

Code:
echo "echo `date`"
echo Thu Sep 10 08:15:31 EDT 2009
echo `echo \`date\``
Thu Sep 10 08:15:12 EDT 2009

  #4 (permalink)  
Old 09-20-2009
raja_kolluru raja_kolluru is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 2
Quote:
Originally Posted by mokachoka View Post
Bit of a weird one i suppose, i want to use an echo inside an echo... For example...

i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos...

echo "echo "hello"" >$file
echo "echo "goodbye"" >$file

Why is this not correct?

Thanks.
A few observations here:
1. As suggested earlier, either "escape" the double quotes surrounding "hello" or use single quote i.e. use

Code:
 echo "echo \"Hello\" "
or echo "echo 'hello' "

2. By doing >$file two times the second echo (i.e. good bye) will over write $file. Instead use

Code:
 echo "echo 'goodbye'" >> $file

to append the contents of the echo into the file.
3. It is a best practice to refrain from creating elaborate scripts using the echo command. Instead use shell "here documents"

Code:
cat <<! > $file
echo "Hello" 
echo "Good bye" 
!

is certainly a lot cleaner.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0