LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-04-2015, 10:19 AM   #1
TheBigMing
Member
 
Registered: Dec 2008
Location: east anglia
Distribution: SuSE, antiX
Posts: 36

Rep: Reputation: 7
The behaviour of single & double quotes in writing & running aliases?


This isn't a problem, more a how or a why:

I have always written an alias (alias ln="ln -sv") which changes the instruction for a "hard" link into one for a "soft". This worked fine until I needed a hard link, so, as part of the alias, I decided to give myself a reminder of what I was doing. After a bit of a struggle, I ended up with this (which I stuffed into my .bash_aliases file):

alias ln="echo -e 'Symbolic (soft) Link (use \033[1;36mhln\033[0m for Hard Link)\nuse \033[1;36m-f\033[0m for force'; ln -sv "

and it works:

Symbolic (soft) Link (use hln for Hard Link )
use -f for force
ln: missing file operand
Try 'ln --help' for more information.

(OK, no operand was supplied here, but the stuff we're talking about works fine).

Recently, however, I needed to look at the construction of the alias, ran "alias ln" & got:

alias ln='echo -e '\''Symbolic (soft) Link (use \033[1;36mhln\033[0m for Hard Link)\nuse \033[1;36m-f\033[0m for force'\''; ln -sv'

Now, I can (sort of) understand the logic of what's going on here, but not entirely - viz:

..1...'...............Open 'protected' mode (was double quote)
........echo -e....Command: invoke "echo -e"
..2...'...............matches 1: Closes 'protected' mode
........\.............invoke newline(\)? Or is this simply an scape character? (Yes it is see http://www.linuxquestions.org/questi...-quote-861714/)
..3...'...............Reopen 'protected' mode (or an escaped single quote - if so why?)
..4...'...............0pens 'protected' mode for "message" code
........Symbolic (soft) Link (use \033[1;36mhln\033[0m for Hard
........Link)\nuse \033[1;36m-f\033[0m for force
..5...'...............5 matches 4 (closes message code),
........\.............invoke newline(\) - or see above
..6...'...............6 matches 3 & closes the section started @ 'reopen'
..7...'...............Reopens 'protected' mode
........; ln -sv......New command[ ; ], creates link
..8...'...............8 matches 7 - the end (or is it?)

I would guess that the "alias" command has some built built in function that does all this - I have saved the the modified version as an alias & it works fine - but although I have read the excellent post:

http://www.linuxquestions.org/questi...-quote-861714/

I don't understand why the system has both double & single quotes. I have always felt that as long as the command line was explained & demonstrated in simple language it was easier to work with that than any point & click system. I grew up with CPM, DOS, Qemm, Desqview, WP5.1, DbII and these were great programs with great (especially WP51) manuals - but, ahh! - those days have gone

Any suggestions gratefully received

dmk
 
Old 04-04-2015, 11:08 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,152

Rep: Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353Reputation: 1353
You are not parsing that output correctly. The 4-character sequence "'\''" is what must be done to represent a literal single quote character embedded within a single-quoted string. Within a single-quoted string, all characters except single quote are taken literally, so that leaves no way to escape that literal single quote. So, the sequence is
Code:
1.  '      end the previous single-quoted string
2.  \'     insert an escaped single quote
3.  '      re-open the single-quoted string
The output is formatted with a single-quoted string simply because that's how aliases are always displayed, regardless of how you wrote them or if some other form might be simpler to read. What you wrote was parsed into an internal form. The particulars of how it got into that form are not entirely preserved.
 
1 members found this post helpful.
Old 04-05-2015, 04:01 PM   #3
TheBigMing
Member
 
Registered: Dec 2008
Location: east anglia
Distribution: SuSE, antiX
Posts: 36

Original Poster
Rep: Reputation: 7
@rknichols - That's great, I'd almost got there but not quite. Thanks a lot!

dmk
 
  


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
[SOLVED] Lost with single and double quotes with bash! Fnux Ubuntu 2 04-01-2013 12:38 AM
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 04:03 PM
gvim - double quote char always replaced with two single quotes in special modes JazzItSelf Linux - Software 4 09-16-2008 12:26 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 12:41 PM
In BASH shell, what is the difference in usage between single and double quotes? davidas Linux - Newbie 2 04-05-2004 04:00 AM


All times are GMT -5. The time now is 12:23 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