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

Ryno picture Ryno · Jul 5, 2014 · Viewed 13.6k times · Source

i'm trying to cut a string into the shell. I'd like to do something like:

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

but the problem is that cut accept a file, so if i pass the string to it, it tries to open the unexistent file called "hello 12345 xyz" and then tries to cut its content

I'd like to resolve this problem with the base programs, so don't tell me to use awk

thanks!

Answer

anubhava picture anubhava · Jul 5, 2014

You can use Here Strings in BASH:

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