Quantcast
Channel: How to get the first word of a string? - Unix & Linux Stack Exchange
Browsing latest articles
Browse All 9 View Live

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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer by glenn jackman for How to get the first word of a string?

You can use the positional parametersset -- *echo "$1"

View Article

Answer 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 Article


How 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

Browsing latest articles
Browse All 9 View Live


Latest Images