Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a word from a string bash

Tags:

bash

I have the string

file="this-is-a-{test}file" 

I want to remove {test} from this string. I used

echo $file | sed 's/[{][^}]*//'  

but this returned me

this-is-a-}file

How can I remove } too?

Thanks

like image 628
Ciprian Vintea Avatar asked Sep 03 '14 07:09

Ciprian Vintea


People also ask

How do I trim a string in Bash?

Example-2: Trim string data using `sed` commandUse sed 's/^ *//g', to remove the leading white spaces. There is another way to remove whitespaces using `sed` command. The following commands removed the spaces from the variable, $Var by using `sed` command and [[:space:]]. $ echo "$Var are very popular now."

What is $@ in Bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.


1 Answers

Also try this bash only oneliner as an alternative:

s="this-is-a-{test}file"
echo ${s/\{test\}/}
like image 197
Shinnok Avatar answered Sep 20 '22 01:09

Shinnok