1

Most of the times, I find that I'm doing:

% chmod +x <file> #or +r or +w

So, I thought about creating an alias such that:

% alias +x='chmod +x' # or
% alias \+x='chmod +x' # or
% alias '+x'='chmod +x'

but it results in:

zsh: bad option: -x

Is there any other method to create the above alias? I currently am setting them up as

% alias c+x='chmod +x'

which is not what I had in mind.

5
  • 1
    have you tried escaping the + with \+? most special characters in filenames must be escaped. that may make your alias clumbsy to use however. cyberciti.biz/faq/… this is imporant because Bash uses + as a reserved operator: tldp.org/LDP/abs/html/special-chars.html Ultimately, I'm forced to conclude that using + in an alias is a bad choice. Jul 5, 2016 at 19:19
  • @FrankThomas Yes, I did that as well. That creates \+x as the alias.
    – hjpotter92
    Jul 5, 2016 at 19:22
  • thats 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. Jul 5, 2016 at 19:25
  • @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
    Jul 5, 2016 at 19:33
  • @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. Jul 5, 2016 at 21:31

1 Answer 1

3

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .