Skip to content

Commit

Permalink
[v1.1] add &>>foo as a shorthand for >>foo 2>&1 (re: 921bbca)
Browse files Browse the repository at this point in the history
This is the 'append' variant of &>. It is very simple to add with
a little code in the lexer (lex.c); all we need to do is xor SYMREP
into the token to signify that the > is repeated. The parser will
then do the right thing without modification.

&>> msut be disabled in the POSIX compatibility mode for the same
reason as &> because, POSIXly,
	somecommand &>>somefile
is the same as
	somecommand &
	>>somefile
so it's a syntactical incompatibility. It's not one that's likely
to cause problems in real-life scripts, but better safe than sorry.
  • Loading branch information
McDutchie committed Dec 5, 2024
1 parent c1dfa1f commit caf8959
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ANNOUNCE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ New command line editor features:

New shell language features:

- The appending redirection operator &>>FILE is now available. It is a
shorthand for: >>FILE 2>&1

- The 'time' reserved word now outputs timings in a precision of 3 digits
after the decimal point if the TIMEFORAMT variable is not set. See also
TIMEFORMAT under 'New features in shell variables' below.
Expand Down
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library.

2024-12-05:

- [v1.1] The appending redirection operator &>>FILE is now available
(inspired by bash 4.0 and later). It is a shorthand for: >>FILE 2>&1

- Fixed an issue in the vi line editors where lines beginning with whitespace
could not be recalled from the history (e.g. with arrow up). Bug introduced
on 2022-10-31.
Expand Down
11 changes: 6 additions & 5 deletions src/cmd/ksh93/sh.1
Original file line number Diff line number Diff line change
Expand Up @@ -3926,10 +3926,11 @@ for reading and store
the file descriptor number in variable
.BR n .
.PP
A special shorthand redirection operator \fB&>\fR\fIword\fR is available; it
is equivalent to \fB>\fR\fIword\fR\fB 2>&1\fR. It cannot be preceded by any
digit or variable name. This shorthand is disabled if the \fBposix\fR shell
option is active.
Special shorthand redirection operators \fB&>\fR\fIword\fR and
\fB&>>\fR\fIword\fR are available; they are equivalent to
\fB>\fR\fIword\fR\fB 2>&1\fR and \fB>\fR\fIword\fR\fB 2>&1\fR, respectively.
They cannot be preceded by any digit or variable name. These shorthands are
disabled if the \fBposix\fR shell option is active.
.PP
The order in which redirections are specified is significant.
The shell evaluates each redirection in terms of the
Expand Down Expand Up @@ -8065,7 +8066,7 @@ and
to open file descriptors > 2 without the close-on-exec flag,
so that they are left open when invoking another program;
.IP \[bu]
disables the \fB&>\fR redirection shorthand;
disables the \fB&>\fR and \fB&>>\fR redirection shorthands;
.IP \[bu]
disables fast filescan loops of type
\f3while\fP \f2inputredirection\^\fP \f3;do\fP \f2list\^\fP \f3;done\fP;
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/ksh93/sh/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,12 @@ int sh_lex(Lex_t* lp)
/* bash-style "&>file" shorthand for ">file 2>&1" */
lp->digits = -1;
c = '>';
/* bash 4.0-style "&>>file" shorthand for ">>file 2>&1" */
fcgetc();
if(fcpeek(0)==c)
c |= SYMREP;
else
fcseek(-1);
}
else if(n=='|')
c |= SYMPIPE;
Expand Down
17 changes: 16 additions & 1 deletion src/cmd/ksh93/tests/posix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,32 @@ got=$(set --noposix; redirect 3>&1; "$SHELL" -c 'echo ok >&3' 2>/dev/null)
[[ $got == "$exp" ]] || err_exit "file descriptor 3 left open in --noposix mode" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"

# disables the &> redirection shorthand;
# disables the &> and &>> redirection shorthands;
exp=''
(set --posix; eval 'echo output &>out') >/dev/null
got=$(<out)
[[ $got == "$exp" ]] || err_exit "&> redirection shorthand not disabled in --posix mode" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
(set --posix; eval 'echo output &>>out') >/dev/null
got=$(<out)
[[ $got == "$exp" ]] || err_exit "&>> redirection shorthand not disabled in --posix mode" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
(set --noposix; eval 'echo output &>out') >/dev/null
exp='output'
got=$(<out)
[[ $got == "$exp" ]] || err_exit "&> redirection shorthand disabled in --noposix mode" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
case ${.sh.version} in
*93u+m/1.0.*)
;;
*) # &>> available as of 93u+m/1.1
(set --noposix; eval 'echo MOAR &>>out') >/dev/null
exp+=$'\nMOAR'
got=$(<out)
[[ $got == "$exp" ]] || err_exit "&>> redirection shorthand disabled in --noposix mode" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
;;
esac

# disables fast filescan loops of type 'while inputredirection; do list; done';
printf '%s\n' "un duo tres" >out
Expand Down

0 comments on commit caf8959

Please sign in to comment.