diff --git a/src/uu/env/locales/en-US.ftl b/src/uu/env/locales/en-US.ftl index 60f9261816e..c6675cae817 100644 --- a/src/uu/env/locales/en-US.ftl +++ b/src/uu/env/locales/en-US.ftl @@ -33,6 +33,7 @@ env-error-use-s-shebang = use -[v]S to pass options in shebang lines env-error-cannot-unset = cannot unset '{ $name }': Invalid argument env-error-cannot-unset-invalid = cannot unset { $name }: Invalid argument env-error-must-specify-command-with-chdir = must specify command with --chdir (-C) +env-error-must-specify-command-with-argv0 = must specify command with --argv0 (-a) env-error-cannot-change-directory = cannot change directory to { $directory }: { $error } env-error-argv0-not-supported = --argv0 is currently not supported on this platform env-error-failed-set-signal-action = failed to set signal action for signal { $signal }: { $error } diff --git a/src/uu/env/src/env.rs b/src/uu/env/src/env.rs index 61efcbcaf99..1ba7aa9b71f 100644 --- a/src/uu/env/src/env.rs +++ b/src/uu/env/src/env.rs @@ -814,6 +814,15 @@ impl EnvAppData { } } + // GNU env requires a command when --argv0 is given. Validate before + // apply_change_directory performs its side effect. + if opts.program.is_empty() && opts.argv0.is_some() { + return Err(UUsageError::new( + 125, + translate!("env-error-must-specify-command-with-argv0"), + )); + } + apply_change_directory(&opts)?; if opts.program.is_empty() { // no program provided, so just dump all env vars to stdout diff --git a/tests/by-util/test_env.rs b/tests/by-util/test_env.rs index 3599e9a6012..cbc4f387cc0 100644 --- a/tests/by-util/test_env.rs +++ b/tests/by-util/test_env.rs @@ -477,6 +477,17 @@ fn test_fail_change_directory() { assert!(out.contains("env: cannot change directory to ")); } +#[test] +fn test_argv0_without_command_fails() { + for arg in ["-a", "--argv0"] { + let out = new_ucmd!() + .args(&[arg, "foo"]) + .fails_with_code(125) + .stderr_move_str(); + assert!(out.contains("must specify command with --argv0 (-a)")); + } +} + #[cfg(not(windows))] // windows has no executable "echo", its only supported as part of a batch-file #[test] fn test_split_string_into_args_one_argument_no_quotes() {