(
and )
are special token characters in the syntax of bash
that are used in a number of operators including:
(...)
subshell construct
func() compound-command
function definitions
$(...)
command substitution
<(...)
, >(...)
process substitution
((...))
arithmetic evaluation construct
$((...))
arithmetic expansion
a=(...)
, a+=(...)
array assignment operators
@(...)
, +(...)
, *(...)
, ?(...)
glob operators (with extglob)
[[ (a && b) || (c && d) ]]
grouping conditional expression operators
[[ ... =~ ...(...)... ]]
regexp operator.
echo a=(b)
is a syntax error, but not export a=(b)
.
echo a)
is a syntax error, unless there was an opening (
in the previous lines part of one of the constructs above.
While it may be possible to write a readline hook that adds quotes where needed around (
, )
to avoid a syntax error, it would be considerable effort as it would mean doing a full parsing of the shell syntax.
A better approach may be to use a shortcut that quotes the current word when you realise too late that it contains characters special to the shell.
With zsh
(assuming emacs
mode):
bindkey -s '\e#' '\C@\eb\Cx\Cx\e"'
To have Alt+# quote the current word.
Or an approximation with bash
(also assuming emacs
mode):
bind "\"\e#\": \"'\e \eb'\C-X\C-X\""
echo bar\(1,3\)