Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I 'echo *' I get the following output:

file1 file2 file3 ...

What I want is to pick out the first word. How can I proceed?

share|improve this question
Assuming this wasn't just an example, to get the first file in a directory, use ls | head -1. –  mattdm Feb 24 '13 at 15:41
@mattdm Using ls won't work if one of the filenames contains a blank. –  helpermethod Feb 25 '13 at 17:07

3 Answers 3

You can pipe it through awk and make it echo the first wird

echo * | head -n1 | awk '{print $1;}'

or you cut the string up and select the first word:

echo *  | head -n1 | cut -d " " -f1

or you pipe it thorugh sed and have it remove everything but the first word

echo * | head -n1 | sed -e 's/\s.*$//'

edit: added the | head -n1 to satisfy nitpickers. In case your string contains newlines | head -n1 will select the first line first before the important commands select the first word from the string passed to it.

share|improve this answer
The would return the first word of every line, not the first word. –  Stéphane Chazelas Feb 24 '13 at 20:19
i wonder how many lines echo * generates –  Bananguin Feb 25 '13 at 14:10
That depends how many files have newline characters in their name or depending on the environment or how bash was compiled or whether some file called -e or -ee... appears in the list, how many time \n appears in a file name. If there's a file called -n, it might not even return any line at all... –  Stéphane Chazelas Feb 25 '13 at 15:05
Well, we're still talking bash and 1. usually bash will not pass a string with newlines as one string with new lines 2. it's very hard and unusual (impossible?) to have a newline in a filename 3. a \n in a filename will show up as a \n 4. 3 holds for filenames starting with - 5. even when called with -n or -e echo will open stdout and close it when it's done so of course it will return a line, and at least one string, for that matter 6. i edited my advice to at least take care of the multiline problem –  Bananguin Feb 25 '13 at 16:34
All 5 points are false. Try in an empty dir: touch '$a\nb' 'a\nb'; env BASHOPTS=xpg_echo bash -c 'echo * | wc -l' (xpg_echo is enabled wherever bash is required to be Unix conformant). And in another empty directory: touch ./-n; bash -c 'echo * | wc -l'. A line is a sequence of characters terminated by a newline character. If echo doesn't output a newline character, it doesn't output any line. Behavior of text utilities like cut, awk or sed is unspecified if the input has extra characters after the last newline character and behavior varies across implementations. –  Stéphane Chazelas Feb 25 '13 at 17:52

You can use the positional parameters

set -- *
echo "$1"
share|improve this answer
Be aware that this will destroy any other arguments to your script, unless run in an auxiliary scope. It will also expand to * if there are no files in the directory. –  Chris Down Feb 24 '13 at 12:47
shopt -s nullglob would handle that –  glenn jackman Feb 24 '13 at 21:13

Assuming that you really want the first filename and not the first word, here's a way that doesn't break on whitespace:

shopt -s nullglob
echo "${files[0]}"
share|improve this answer
First word would be easy, too: files=($(echo *)) –  Hauke Laging Feb 24 '13 at 12:54
It would break on "-n", "-e", "-ne", "-Enenene"..., and depending on how bash was compiled or the environment, possibly on backslash characters, though. –  Stéphane Chazelas Feb 24 '13 at 20:18
@HaukeLaging, you'd need to disable globbing. Like: text=$(echo *); set -f; files=($text), otherwise more wildcards could be expanded. –  Stéphane Chazelas Feb 24 '13 at 20:23
and files=$(echo *); echo ${files%% *} –  발렌텐 Feb 27 '13 at 19:41
@memnoch_proxy It will break on filenames containing whitespace, use it with caution. –  Chris Down May 22 '13 at 2:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.