Shell: How to cut a single string with "Cut"? Shell: How to cut a single string with "Cut"? shell shell

Shell: How to cut a single string with "Cut"?


You can use Here Strings in BASH:

cut -d' ' -f1 <<< "hello 12345 xyz"hello


There are several ways to do this in bash itself. If your string is already in a parameter (or you don't mind assigning it to one, you can use read:

str="hello 12345 xyz"read first rest <<< "$str"

or parameter expansion:

echo "${str% *}"

or regular expression matching:

[[ "hello 12345 xyz" =~ ([^ ]+)\  ]]echo "${BASH_REMATCH[1]}"


To get cut to act on a string instead of a file, you have to give it the string via STDIN. The most common way to do so is this:

$ echo 'hello 12345 xyz' | cut -f 1hello