Help answer threads with 0 replies.
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 03-31-2016, 11:55 AM   #1
LQ Newbie
Registered: Jun 2013
Posts: 2

Rep: Reputation: Disabled
shell: how to escape (or not escape) $ within an echo statement?

I'm new to shell programing, and what I'm trying to do may be a little silly, but I'd like to use ECHO within the shell to build a Perl script that will be later executed by that same shell program. the problem i'm having is how to escape the $ character within the echo line to get what i want, as well as issues of quotes within quotes.

so here's a piece of my shell script

# make the date string for yesterday
NOW=$(date -d "1 day ago" '+%y%m%d')
# make a line to put in perl file
echo "$ftp->put("/home/pi/bin/log$NOW.txt") || die $ftp->message;" >>

if today is Mar 30, then the value of $NOW is 160329 (the day before)

and what I want to go into the file "" from line 4 in my script is:

$ftp->put("/home/pi/bin/log160329.txt") || die $ftp->message;

so I need to somehow get "$ftp-put" and "$ftp->message" to be output just as the are, and yet I also want $NOW to be converted to 160329.

so how to I escape $ftp-put and ftp->message so that that's what is produced, and how to I quote this line? single quotes, double quotes? and how to I escape the quotes so that I get quotes in the output file when I want them?

thanks for any advice offered.

Last edited by Paul_N; 03-31-2016 at 12:03 PM.
Old 03-31-2016, 12:13 PM   #2
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,768

Rep: Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050
Backslash (\) is the escape character for many languages, including bash
1 members found this post helpful.
Old 04-05-2016, 02:59 AM   #3
LQ Addict
Registered: Dec 2013
Posts: 13,390
Blog Entries: 9

Rep: Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652Reputation: 3652
echo "\$ftp->put("/home/pi/bin/log$NOW.txt") || die \$ftp->message;" >>
you might need to escape more characters, i don't know.
the way you are going about this seems overly complex, but if it's just a brain teaser, why not...

otoh, i once made a shell script that produces conky syntax, and to me it's very useful. lots of escaped $ in there.


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
Shell Scripting escape sequences cmillosh Linux - General 3 10-05-2011 10:29 AM
want to escape forward slash, i tired but not working.. statement garbled vaibhavs17 Linux - Newbie 4 05-02-2011 01:24 PM
escape '*' in case statement bash junust Programming 2 01-23-2010 04:54 AM
echo escape character weirdness insecurityman Programming 7 10-20-2008 05:20 AM
How do I escape $# in shell scripting? KDE4me Programming 3 06-25-2003 11:35 PM > Forums > Linux Forums > Linux - Newbie

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