Search code examples
bashpipeheadlsio-redirection

Using output of head -1


When I write

ls | head -1

the output is

file.txt

When I write

ls | head -1 > output.txt or

echo `ls | head -1` > output.txt

the file output.txt contains

^[[H^[[2Jfile.txt

This makes me trouble because I need to use the output of head -1 as an argument of another command.

How can I achieve this?


Solution

  • Possibly your ls is aliased to something like ls --color=always. Try /bin/ls | head -1 > output.txt