简体   繁体   中英

Get only part of uname -r in bash

I need to get only first two numbers of "uname -r" command in bash

example of regular out put:

uname -r
3.5.0-18-generic

what I expect using magic bash options:

3.5

assuming you want everything before the second dot, this will do what you want:

uname -r | cut -d. -f1-2

uname itself does not support cutting the output, afaik. The pipe through cut will show you fields 1 and 2 ( -f1-2 ), delimited by dots ( -d. )

uname -r | sed 's/\([0-9]\+\.[0-9]\+\)\..*/\1/'

You could also accomplish this with parameter expansion:

$ r="$(uname -r)"
$ echo ${r%.*}
3.5

${VAR%pat} non-greedily removes pat from the end of VAR . Note that pat is a glob pattern ie dot just means "dot" and star means "any-number-of-chars".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM