diff --git a/src/builtins/read.cpp b/src/builtins/read.cpp index 0e7574b41..72b176af8 100644 --- a/src/builtins/read.cpp +++ b/src/builtins/read.cpp @@ -424,6 +424,11 @@ static int validate_read_args(const wchar_t *cmd, read_cmd_opts_t &opts, int arg builtin_print_error_trailer(parser, streams.err, cmd); return STATUS_INVALID_ARGS; } + if (env_var_t::flags_for(argv[i]) & env_var_t::flag_read_only) { + streams.err.append_format(_(L"%ls: %ls: cannot overwrite read-only variable"), cmd, argv[i]); + builtin_print_error_trailer(parser, streams.err, cmd); + return STATUS_INVALID_ARGS; + } } return STATUS_CMD_OK; diff --git a/tests/checks/read.fish b/tests/checks/read.fish index 709ee977b..1172a6e70 100644 --- a/tests/checks/read.fish +++ b/tests/checks/read.fish @@ -380,3 +380,12 @@ begin end | read -l --line foo bar echo $foo $bar # CHECK: 1 2 + +echo foo | read status +# CHECKERR: read: status: cannot overwrite read-only variable +# CHECKERR: {{.*}}read.fish (line {{\d+}}): +# CHECKERR: echo foo | read status +# CHECKERR: ^ +# CHECKERR: (Type 'help read' for related documentation) +echo read $status +# CHECK: read 2