diff --git a/src/uu/ps/src/ps.rs b/src/uu/ps/src/ps.rs index 5ee1af3c..8ee51cab 100644 --- a/src/uu/ps/src/ps.rs +++ b/src/uu/ps/src/ps.rs @@ -113,7 +113,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let mut table = Table::new(); table.set_format(*FORMAT_CLEAN); - if !matches.get_flag("no-headers") { + // As in procps-ng, the header line is left out when every header is + // empty, e.g. `ps -o pid= -o comm=`. + let all_headers_empty = code_mapping.iter().all(|(_, header)| header.is_empty()); + if !matches.get_flag("no-headers") && !all_headers_empty { let header = code_mapping .iter() .map(|(_, header)| header) diff --git a/tests/by-util/test_ps.rs b/tests/by-util/test_ps.rs index d6bb2531..9af72dbd 100644 --- a/tests/by-util/test_ps.rs +++ b/tests/by-util/test_ps.rs @@ -204,6 +204,25 @@ fn test_no_headers_flags() { } } +#[test] +#[cfg(target_os = "linux")] +fn test_all_empty_headers_print_no_header_line() { + new_ucmd!() + .args(&["-o", "pid=", "-p", "1"]) + .succeeds() + .stdout_matches(&Regex::new(r"\A *1 *\n\z").unwrap()); + new_ucmd!() + .args(&["-o", "pid=", "-o", "ppid=", "-p", "1"]) + .succeeds() + .stdout_matches(&Regex::new(r"\A *1 +0 *\n\z").unwrap()); + + // One named header keeps the header line. + new_ucmd!() + .args(&["-o", "pid=", "-o", "ppid", "-p", "1"]) + .succeeds() + .stdout_matches(&Regex::new(r"\A +PPID *\n *1 +0 *\n\z").unwrap()); +} + #[test] #[cfg(target_os = "linux")] fn test_deselect() {