1

It is known that the character (!) Is reserved, the curious thing is that it works on the common user, but does not work as root, I tried some approaches to solve my problem, but without success, I would like to know if it is possible to escape these characters .

The command I'm trying to execute is simple, a move ignoring some specific files.

 mkdir --parents www/; mv !(init.sh|environment.sh|docs|www) $_
3
  • Do you really want to escape it - or do you want it to be interpreted as a KSH-style extended shell glob? Perhaps the issue is that the root shell is different from that of your common user (a different shell, or the appropriate shell option not set)? Nov 16, 2020 at 13:56
  • I didn't know KSH, did a quick search, installed the package, and with a little adaptation it worked for me. In that case what should I do, answer the question with the solution or delete it? Nov 16, 2020 at 14:10
  • 3
    The command would have worked in bash too, if the extglob shell option was set, and in zsh if the option KSH_GLOB was set. It's unclear what shell your "common user" is using and how it's configured, and how this differs from the shell that the root user is using.
    – Kusalananda
    Nov 16, 2020 at 14:32

1 Answer 1

2

This has nothing to do with escaping the ! character.

The ksh shell implements some extended globbing patterns, and !(...|...|...) is one of these. The pattern matches anything not matched by any of the patterns in the parenthesis.

Some shells are able to use ksh globbing patterns. For example, setting the extglob shell option in the bash shell (shopt -s extglob) enables these, as does setting the KSH_GLOB shell option in the zsh shell (setopt KSH_GLOB).

The shell that your root user is using obviously does not enable the use of ksh globbing patterns, and it is unclear whether it's even able to (the dash shell, for example, don't have this ability). Your ordinary user, on the other hand, seems to have enabled these patterns by default, either by virtue of running the actual ksh shell, by explicitly enabling the correct shell option in a shell initialization file, or by using some third-party extension that quietly enables the shell option by default.

1
  • 1
    Also note that bash-completion (third party completion extensions for bash) enables extglob which is one way that option may be enabled without one being made aware of it. Nov 17, 2020 at 15:03

You must log in to answer this question.

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