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 '{}' \;'
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 '{}' \;'
You could just use double quotes:
alias mx="find . -name '*.sh' -exec chmod a+x {} \;"
Also, the single quotes '
around the {}
are not necessary.
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 '{}' \;
}
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.
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'