Q:

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?

A:

These are probably terminal escape codes for coloring. Your ls setup seems to be broken, normally coloring should only be done when connected directly to a terminal.
ls --color=never | head -1

should fix the issue.

A:

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

