18

I want to create an alias of this command:

find . -name '*.sh' -exec chmod a+x '{}' \;

And I am not able to escape the single quotes while setting the alias:

alias mx='find . -name '*.sh' -exec chmod a+x '{}' \;'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sumit
  • 1,934
  • 4
  • 17
  • 23
  • 1
    see this http://stackoverflow.com/questions/1250079/bash-escaping-single-quotes-inside-of-single-quoted-strings when you need double quotes and single quotes – yvess Nov 15 '12 at 16:42
  • The canonical is probably *[How to escape single quotes within single quoted strings](https://stackoverflow.com/questions/1250079/)* (2009. 26 answers. 1380 votes). See [this answer](https://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings/16605140#16605140) for a relatively simple syntax. – Peter Mortensen Aug 16 '23 at 18:33

4 Answers4

16

You could just use double quotes:

alias mx="find . -name '*.sh' -exec chmod a+x {} \;"

Also, the single quotes ' around the {} are not necessary.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
atx
  • 4,831
  • 3
  • 26
  • 40
  • Do you also need to escape the backslash since you're using double quotes? `"... chmod a+x {} \\\;"` – glenn jackman May 12 '11 at 10:42
  • Works well. FYI, when I create an alias like that, (GNU bash, version 4.3.11(1)) it automatically changes the double-quotes into single quotes, and converts any single quotes into `'\''` . So the mx alias above becomes `alias mx='find . -name '\''*.sh'\'' -exec chmod a+x {} \;'` all by itself, similar to Mark's answer (also good). – Xen2050 Jun 29 '15 at 01:18
14

You want a function, not an alias.

function mx {
  find . -name '*.sh' -exec chmod a+x '{}' \;
}

This will have the same effect an alias would have had, avoids any "creative solutions" to make work, and is more flexible should you ever need the flexibility. A good example of this flexibility, in this case, is enabling the user to specify the directory to search, and default to the current directory if no directory is specified.

function mx {
  if [ -n $1 ]; then
    dir=$1
  else
    dir='.'
  fi
  find $dir -name '*.sh' -exec chmod a+x '{}' \;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cledoux
  • 4,717
  • 1
  • 22
  • 30
12

The other answers contain better solutions in this (and most) cases, but might you for some reason really want to escape ', you can do '"'"' which actually ends the string, adds a ' escaped by " and starts the string again.

alias mx='find . -name '"'"'*.sh'"'"' -exec chmod a+x {} \;'

There is more information at How to escape single quotes within single quoted strings.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark
  • 18,730
  • 7
  • 107
  • 130
1

Try

alias mx=$'find . -name \'*.sh\' -exec chmod a+x \'{}\' \\;'

From man bash:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

\\     backslash
\'     single quote
\"     double quote
\n     new line
...

See example:

echo $'aa\'bb'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mj41
  • 2,459
  • 2
  • 16
  • 10