xralf
xralf

Reputation: 3702

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?

Upvotes: 3

Views: 5490

Answers (2)

unbeli
unbeli

Reputation: 30248

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

Upvotes: 3

thiton
thiton

Reputation: 36059

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.

Upvotes: 3

Related Questions