开发者

How to escape single quote while setting aliases

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 开发者_如何学JAVAwhile setting the alias

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

Any help is appreciated.


You could just use double quotes:

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

EDIT: Also, the single quotes ' around the {} is not necessary.


What you want is 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 '{}' \;
}


The other answers contain better solutiuons 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 {} \;'

More info at How to escape single-quotes within single-quoted strings?


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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜