This question already has an answer here:

I am not able to set alias of hexdump -e '/1 "%_ax) "' -e '/1 "%02X" "\n"'

I have tried following methods but failed:

alias analyze=\'hexdump -e '/1 "%_ax) "' -e '/1 "%02X" "\n"'\'

(note the first and last single quotes, I have escaped them both)

alias analyze='hexdump -e \'/1 "%_ax) "\' -e \'/1 "%02X" "\n"\''

(note in this case I have escaped all 4 single quotes in between the command)

alias analyze=hexdump -e '/1 "%_ax) "' -e '/1 "%02X" "\n"'

(not enclosing the command with quotes at all)

How do I set alias of hexdump -e '/1 "%_ax) "' -e '/1 "%02X" "\n"' in bash?

marked as duplicate by don_crissti, garethTheRed, Jakuje, Anthon, cas Apr 30 '16 at 11:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Another solution to the underlying problem: use a bash function instead of an alias analyze() { hexdump -e 'this' -e 'that' "$@"; } – dave_thompson_085 Apr 30 '16 at 10:51
  • @dave_thompson_085 does not work when I pipe data in to it, error: hexdump: bad format {this} – Edward Torvalds Apr 30 '16 at 15:12
  • 'this' and 'that' should be the two singlequoted format strings you want (containing doublequotes), I didn't want to retype that cruft and maybe make a typo. My point is you already have quoting that works on commandline, and can use exactly the same in a function, but alias requires more complicated quoting. – dave_thompson_085 May 1 '16 at 6:34

Rule of thumb, if escaping one kind of quotes doesn't work, escape the other:

alias analyze="hexdump -e '/1 \"%_ax) \"' -e '/1 \"%02X\" \"\n\"'"

Here, I have escaped the inner double quotes, and quoted everything with double quotes.

The complete rule seems to be that you can escape double quotes inside double quotes, but you cannot escape single quotes inside single quotes. You have to escape the outside ones, but it doesn't work when defining an alias.

$ alias hi='echo hi'        #works
$ alias hi='echo "hi"'      #works
$ alias hi='echo \"hi\"'    #works
$ alias hi="echo \'hi\'"    #works
$ alias hi="echo \"hi\""    #works
$ alias hi='echo \'hi\''    #doesn't work
$ alias hi=\'echo 'hi'\'    #should work but doesn't

Once you have defined the alias correctly, you can ask bash for what was the correct answer with command alias which lists all the aliases. You can cheat and use @meuh's method if you cannot find the correct quoting. In this case:

$ alias
alias analyze='hexdump -e '\''/1 "%_ax) "'\'' -e '\''/1 "%02X" "\n"'\'''
  • how can this be working? aren't single quote and double quote same? – Edward Torvalds Apr 30 '16 at 8:32
  • 1
    @edwardtorvalds Certainly not. In bash, they have distinct usages. Double quotes interpolate the strings, while single quotes leave them as-is. Try echo "$HOME" versus echo '$HOME' for example. – lgeorget Apr 30 '16 at 8:33
  • @don_crissti Thank you. I'm definitely not bash-savvy enough to understand this and I don't know if I want o be >.< I'll just use double-quotes instead. :D – lgeorget Apr 30 '16 at 9:00

You can also get bash to do the quote escaping for you, eg

read -r  <<\!
hexdump -e '/1 "%_ax) "' -e '/1 "%02X" "\n"'
alias analyze="$REPLY"
  • Very clever. This way, we can know the proper way to quote with single quotes. – lgeorget Apr 30 '16 at 8:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.