• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

How To Append Text to Variable & SED

I want to add ' in front and behind my variable and replace one character inside.

Here is my input: peter:chris
Here is my expected output: 'peter-chris'

I have tried, but my code is failing. My echo prints nothing. Code attempt attached.

Appreciate your help.

if [ -n "$string" ]
then
	set string = `echo "'"$string"'" | sed "s/:/-/"`
	echo $string;
fi

Open in new window

0
Neapolitan23
Asked:
Neapolitan23
  • 2
1 Solution
 
ozoCommented:
Which shell are you using, and how did you set $string before running that code?
0
 
Neapolitan23Author Commented:
bash shell.

string="$1" (value from the command line)
0
 
ozoCommented:
In that case, I would have expected the output

peter:chris

and I don't understand why your echo prints nothing

If I change
   set string = `echo "'"$string"'" | sed "s/:/-/"`
to
   string=`echo "'"$string"'" | sed "s/:/-/"`
then I get
'peter-chris'

Although a simpler way to do that could be
echo "'${string/:/-}'"
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now