Answer by Safayet Ahmed for How to get the first word of a string?
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]}
View ArticleAnswer by markle for How to get the first word of a string?
Getting the whole first file name:shopt -s nullglobprintf '%s\000' * | grep -z -m 1 '^..*$'printf '%s\000' * | ( IFS="" read -r -d "" var; printf '%s\n'"$var" )
View ArticleAnswer by Alexander for How to get the first word of a string?
This works:echo * | grep -o "^\w*\b"Creds to https://unix.stackexchange.com/a/57879/3920
View ArticleAnswer by kenorb for How to get the first word of a string?
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...
View ArticleAnswer by starfry for How to get the first word of a string?
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...
View ArticleAnswer by Bananguin for How to get the first word of a string?
You can pipe it through awk and make it echo the first wordecho * | head -n1 | awk '{print $1;}'or you cut the string up and select the first word:echo * | head -n1 | cut -d "" -f1or you pipe it...
View ArticleAnswer by glenn jackman for How to get the first word of a string?
You can use the positional parametersset -- *echo "$1"
View ArticleAnswer by Chris Down for How to get the first word of a string?
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 nullglobfiles=(*)printf '%s\n'"${files[0]}"
View ArticleHow to get the first word of a string?
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?
View Article