Quoting +
in that context has not desired effect effect because it only tells zle
(zsh's command line editor) to take +
literally. The command alias
itself still recieves the string "+x" as first command line argument and thus tries to parse it as option.
If you pass the option --
to the alias
command, everything after it will be used as an argument, even if it starts with an +
or -
. To use your example:
alias -- +x='chmod +x'
alias -- -x='chmod -x'
Note: Many - but not all - other tools behave similarly, zsh built-ins as well as external commands. For example:
% touch -negative
touch: invalid option -- 'n'
Try 'touch --help' for more information.
% touch -- -negative
% ls -negative
ls: invalid option -- 'e'
Try 'ls --help' for more information.
% ls -- -negative
-negative
% rm -negative
rm: invalid option -- 'n'
Try 'rm ./-negative' to remove the file '-negative'.
Try 'rm --help' for more information.
% rm -- -negative
Of course, you can also do as rm
suggests and use ./-negative
in most (all?) of these cases where paths are involved.
One notable exception is GNU echo
. It is difficult to get it to print any options it recognizes without additional text. For example "-n":
% /bin/echo -n
% /bin/echo -- -n
-- -n
% /bin/echo -n -e '-n\n'
-n
1
have you tried escaping the + with
– Frank Thomas – 2016-07-05T19:19:10.473\+
? most special characters in filenames must be escaped. that may make your alias clumbsy to use however. http://www.cyberciti.biz/faq/linuxunix-rules-for-naming-file-and-directory-names/ this is imporant because Bash uses+
as a reserved operator: http://www.tldp.org/LDP/abs/html/special-chars.html Ultimately, I'm forced to conclude that using+
in an alias is a bad choice.@FrankThomas Yes, I did that as well. That creates
\+x
as the alias. – hjpotter92 – 2016-07-05T19:22:38.243thats likely as close as you are going to get. the parser for BASH is going to assume that a leading + is an operator, either for math or regular expressions. – Frank Thomas – 2016-07-05T19:25:50.807
@FrankThomas I'm in zsh, if that makes any difference. I understand that
+
is an operator in shell, I was just wondering whether creating+x
alias is allowed, since trying to execute+x
raises a command not found error. – hjpotter92 – 2016-07-05T19:33:19.730@FrankThomas:
alias +x='chmod +x'
works fine in bash; this is a zsh problem. @hjpotter92: This is a kludge, but if you get a "command not found" error when you type+x
, that suggests that you may be able to create a script called+x
in your private bin directory. – Scott – 2016-07-05T21:31:45.397