diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 88af56bb186..6bd62b8389f 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -620,7 +620,9 @@ fn extract_quoting_style(options: &clap::ArgMatches, show_control: bool) -> Quot QuotingStyle::C { quotes: quoting_style::Quotes::Double, } - } else if options.get_flag(options::DIRED) { + } else if options.get_flag(options::DIRED) || !std::io::stdout().is_terminal() { + // By default, `ls` uses Literal quoting when + // writing to a non-terminal file descriptor QuotingStyle::Literal { show_control } } else { // TODO: use environment variable if available diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 07ea8c9cd63..43f3f660044 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -2453,13 +2453,16 @@ fn test_ls_quoting_style() { { at.touch("one\ntwo"); at.touch("one\\two"); - // Default is shell-escape + // Default is literal, when stdout is not a TTY. + // Otherwise, it is shell-escape scene .ucmd() .arg("--hide-control-chars") .arg("one\ntwo") .succeeds() - .stdout_only("'one'$'\\n''two'\n"); + .stdout_only("one?two\n"); + // TODO: TTY-expected output, find a way to check this as well + // .stdout_only("'one'$'\\n''two'\n"); for (arg, correct) in [ ("--quoting-style=literal", "one?two"), @@ -2546,7 +2549,9 @@ fn test_ls_quoting_style() { .ucmd() .arg("one two") .succeeds() - .stdout_only("'one two'\n"); + .stdout_only("one two\n"); + // TODO: TTY-expected output + // .stdout_only("'one two'\n"); for (arg, correct) in [ ("--quoting-style=literal", "one two"), @@ -2609,7 +2614,9 @@ fn test_ls_quoting_and_color() { .arg("--color") .arg("one two") .succeeds() - .stdout_only("'one two'\n"); + .stdout_only("one two\n"); + // TODO: TTY-expected output + // .stdout_only("'one two'\n"); } #[test] @@ -3141,11 +3148,8 @@ fn test_ls_path() { .stdout_is(expected_stdout); let abs_path = format!("{}/{}", at.as_string(), path); - let expected_stdout = if cfg!(windows) { - format!("\'{abs_path}\'\n") - } else { - format!("{abs_path}\n") - }; + let expected_stdout = format!("{abs_path}\n"); + scene.ucmd().arg(&abs_path).run().stdout_is(expected_stdout); let expected_stdout = format!("{path}\n{file1}\n");