4

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.

3
  • Arguments in bash are not separated by any commas. Remove them and you should be fine.
    – pLumo
    May 30 at 10:47
  • I don’t see an array here neither alive nor about to be born … If I’m missing something, please let me know.
    – Raffa
    May 30 at 12:15
  • Well done. Thanks a lot guys. May 30 at 15:22

2 Answers 2

6

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.

6

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."
3
  • For posterity … "leaning toothpicks" i.e. backward slash AKA escape character :-)
    – Raffa
    May 30 at 13:33
  • Upvote for recommending a function. When an alias has problems, switching to a function will usually solve it.
    – Barmar
    May 31 at 16:13
  • @Barmar thanks - in fact, the bash manual itself says "For almost every purpose, shell functions are preferred over aliases. " May 31 at 16:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .