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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityYou can pipe it through awk and make it echo the first word
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.*$//'
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.
-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...
Feb 25, 2013 at 15:05
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.
Feb 25, 2013 at 17:52
Assuming a posixy shell (/bin/sh
or /bin/bash
can do this)
all=$(echo *)
first=${all%% *}
The construct ${all%% *}
is an example of substring removal. The %%
means delete the longest match of *
(a space followed by anything) from the right-hand end of the variable all
. You can read more about string manipulation here.
This solution assumes that the separator is a space. If you're doing this with file names then any with spaces will break it.
head
and cut
echo *
were a stream or some # of lines approaching infinity, wouldn't this be a particularly fun way to OOM your box? I don't if you can OOM of an assignment like this, as in does any shell provide constraints (like buffer + block writes to memory), but my guess is that there arent and that you definitely can write to all available memory on a casual, nothing to see here, assignment.
Apr 3, 2021 at 23:08
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
files=(*)
printf '%s\n' "${files[0]}"
bash
was compiled or the environment, possibly on backslash characters, though.
Feb 24, 2013 at 20:18
text=$(echo *); set -f; files=($text)
, otherwise more wildcards could be expanded.
Feb 24, 2013 at 20:23
You can use the positional parameters
set -- *
echo "$1"
*
if there are no files in the directory.
Feb 24, 2013 at 12:47
-n
, -e
, -ne
, -en
, etc. On some platforms.
Apr 4, 2022 at 8:20
Check one of the following alternatives:
$ FILE=($(echo *))
$ FILE=$(echo * | grep -o "^\S*")
$ FILE=$(echo * | grep -o "[^ ]*")
$ FILE=$(find . -type f -print -quit)
Then you can print it via echo $FILE
.
See also: grep
the only first word from output?
Getting the whole first file name:
shopt -s nullglob
printf '%s\000' * | grep -z -m 1 '^..*$'
printf '%s\000' * | ( IFS="" read -r -d "" var; printf '%s\n' "$var" )
Another approach is to list all the file names as an array, and then index the array for the first element:
STRARRAY=($(echo *))
FIRST=${STRARRAY[0]}
$STRARRAY
because bash automatically selects [0]
Apr 4, 2022 at 1:19
ls
won't work if one of the filenames contains a blank.Sunset on a beach.jpg
, should it beSunset
or the whole file name? What aboutSea, sex and sun.ogg
?Sea
,Sea,
or the whole file name?ls | head -1
gives me random things likea.patch p.py
which is not the first word and not even files in alphabetical order.