简体   繁体   中英

Replace string value using SED command

my command is

sh abc.sh 'hello'
  

Inside my shell script abc.sh

var1=$1
sed -i "s/'/ /g" $var1 

but sed is not working

sed wait filename in argument (see man sed )

sed [OPTION] .... {script-only-if-no-other-script} [input-file]

Into your script, you need to change to:

echo "$var1" | sed "s/'/ /g"

You are performing sed on a variable and not a file and so redirect the variable back into sed to get the desired result:

sed "s/'//g" <<< "$var1"

This avoids the inefficiencies associated with echo "$var1" | sed.....

An alternative to sed in this use case is tr and so:

tr -d "'" <<< "$var1"

Use -d "'" to pass the character you want to delete from the text.

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