简体   繁体   中英

How can I delete multiple characters in my bash script?

I want to create a bash script on Linux, which will only tell me my ip-adress, netmask and broadcast. Right now it shows more than that though, so I would like to remove a specific number of characters from my variable.

Example:

What I have

ip=Hello world!

What I want

ip=Hello

So how can I remove a specific amount of characters from the back of the variable?

I tried multiple things that I found online, but couldn't get it working the way I want it to.

with bash 's substring extraction:

$ my_var="Hello world!"
$ my_var=${my_var:0:-6}
$ echo $my_var
Hello

You could pipe through grep and only output the matching portion

echo "ip=hello world" | grep -o "ip=\w*"

Output: ip=hello

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