1

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?

Alex Jones
  • 6,353
  • 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} – Alex Jones 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 01 '16 at 06:34

2 Answers2

2

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"'\'''
lgeorget
  • 13,914
  • how can this be working? aren't single quote and double quote same? – Alex Jones Apr 30 '16 at 08: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 08: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 09:00
1

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"
meuh
  • 51,383