YorSubs
YorSubs

Reputation: 4050

bash, getting ' characters inside printf statements

I find it hard to nest printf statements inside aliases. I have a number of help topics that I want to have available (just little collections of helpful tips for when I forget syntax). I have found out that printf requires \ to be escaped as \\ and % to be escaped as %%. However, my problem is more to do with ' and "

alias helpx='printf "A note about 'vim'.\n"'
=> A note about vim.   # The ' are ignored.

alias helpx="printf 'A note about 'vim'.\n'"
=> A note about vim.   # The ' are ignored.

alias helpx='printf "A note about \'vim\'.\n"' # Invalid syntax

alias helpx='printf "A note about \"vim\".\n"'
=> A note about "vim".   # Some progress, I can now get " here

How can I get ' characters inside my notes in the above?

Upvotes: 1

Views: 83

Answers (2)

Manifest Man
Manifest Man

Reputation: 905

You can do following to escape character while using printf-command on an unix terminal:

 printf "A note about \'vim\'.\n"

Since you are interested in assigning this command to a variale (your alias "helpx"), you could do it at least in two different ways:

  • If you have affinity to ASCI punctuation & symbols, then without dealing to much with escaping of characters, then use the above solution

    alias helpx='printf "A note about \u0027vim\u0027.\u000A"'
    
  • If you don't have affinity to ASCI punctuation & symbols

    use the answer proposed by @tshiono

Hopefully, this will help also in the future when dealing with such problems.

Upvotes: 1

tshiono
tshiono

Reputation: 22012

Would you please try:

alias helpx='printf "A note about '\''vim'\''.\n"'

or:

alias helpx="printf \"A note about 'vim'.\\n\""

Upvotes: 1

Related Questions