Score:4

How to printf "`" and "%" character inside array on aliasses?

in flag

I have some issues with the button on the keyboard, I have already replaced my keyboard 3 times. I use printf for some text editors temporarily. I need to printf "`" and "%" characters inside the array with colour on aliasses, not working at all.
Basically in bash like this :

printf "[%c%c%c]\n", '`', '`', '%';

The Output :

[``%]

i try this :

alias key='printf "\n\033[33m= "[ %c%c%c~+ |\ ]\n\n", '`', '`', '%'" '

on aliasses i get this error :

]nn, , %: command not found

What format characters are should be on aliasses?

Thanks in advance.

pLumo avatar
in flag
Arguments in bash are not separated by any commas. Remove them and you should be fine.
Raffa avatar
jp flag
I don’t see an array here neither alive nor about to be born … If I’m missing something, please let me know.
abu-ahmed al-khatiri avatar
in flag
Well done. Thanks a lot guys.
Score:6
jp flag

You use backticks "`" in your alias ... Those are used for command substitution in bash ... Although old style and shouldn't be used as they might get obsolete sometime in the future in favor of the current command substitution syntax $( ... ) ... They need to be escaped in order for you to be able to pass them as arguments to printf and as literal backticks like you did ... Like so:

alias key='printf "\n\033[33m [%s%s%s]\n\n" "\`" "\`" "%"'

The other way that worked for you i.e.:

printf "[%c%c%c]\n", '`', '`', '%'

Was because they were enclosed in single quotes which is another way of telling bash not to process them as command substitution markers ... But when you used that in an alias string and added more single quotes, the whole quoting boundaries flipped around and broke open around the backticks i.e.:

alias key='printf "\n\033[33m= "[ %c%c%c~+ |\ ]\n\n", '`', '`', '%'" '

and backticks got processed by bash.

Separating printf's arguments with commas is not needed in bash although should be parsed fine.

Also using the character specifier %c is not usually necessary as the string specifier %s can be used with proper quoting/escaping and should be more flexible.

Score:6
hr flag

Ignoring the extraneous comma (,) characters, your issue here is with quoting.

Backticks are a deprecated syntax for command substitution, and retain that special meaning inside double quotes. So whereas

printf '%c%c\n' '`' '`'

works,

printf '%c%c\n' "`" "`"

is equivalent to printf '%c%c\n' "$(" ")", which attempts to execute a literal space as a command and pass the result as an argument to printf:

$ printf '%c%c\n' "`" "`"
 : command not found

So, why can't you simply define your alias using the single quoted form? Well:

  1. single quotes don't nest, so

    alias key='printf '%c%c\n' '`' '`' ' 
    

    concatenates 'printf ', %c%c\n, and ' ' into a single format string, leaving (unquoted)

    `' '`' '
    

    which your interactive shell expands equivalent to $(' ')' ' - again trying to execute a literal space.

  2. on the other hand, if you try to use outer double quotes

    alias key="printf '%c%c\n' '`' '`' "
    

    then according to the quoting rules linked above, the single quotes become literal, and your shell again expands ' ' as a command.

What does work is to escape the single quote:

$ unalias key
$ alias key='printf "%c%c\n" '\''`'\'' '\''`'\'' '
$ 
$ key
``

For your full alias that would be

alias key='printf "\n\033[33m= [ %c%c%c~+ |\ ]\n\n" '\''`'\'' '\''`'\'' "%" '

(although the quoting of "%" isn't strictly necessary).

If the "leaning toothpicks" become too much to handle, you could consider using a shell function instead of an alias, eliminating one level of quoting1:

$ key () { printf '\n\033[33m= [ %c%c%c~+ |\ ]\n\n' '`' '`' '%' ; }
$ 
$ key

= [ ``%~+ |\ ]

See also In Bash, when to alias, when to script and when to write a function?


  1. in fact, the bash manual's own section on aliases says "For almost every purpose, shell functions are preferred over aliases."
Raffa avatar
jp flag
For posterity … "leaning toothpicks" i.e. backward slash AKA escape character :-)
cn flag
Upvote for recommending a function. When an alias has problems, switching to a function will usually solve it.
hr flag
@Barmar thanks - in fact, the [bash manual itself](https://www.gnu.org/software/bash/manual/html_node/Aliases.html) says *"For almost every purpose, shell functions are preferred over aliases. "*
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.