Below is a complete copy of a demo I’m using to to figure out to get the sed command to get rid of the spaces in a persons name, and compress it down to not have spaces.
Once this is done, I want to assign it to the variable comp then I can re-use it later on in the script. Here I am just trying to echo it to the stdout so I can see it worked.
If I run the script and enter my name as Ronald McDonald the result I get returned is RonaldMcDonald} with that curly brace on the end of his name, or whatever I type in.
How do I get it to work, so that the result doesn’t append the } to the back of the assigned text.
#!/bin/bash
function readName {
echo "Enter your full name:"
read fullName
clear
} # end readName
function cmprsName {
comp={ echo "$fullName" } | sed 's/ //g'
} # end cmprsName
function sayItNow {
echo $comp
} # end sayItNow
function allTogether {
readName
cmprsName
sayItNow
} #end allTogether
case $1 in
-h | --help ) allTogether
exit
;;
* ) echo "$0 -h"
exit 1
esac
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You have to use command substitution for this, i.e. instead of
comp={ echo "$fullName" } | sed 's/ //g'
something like
comp=$(echo "$fullName" | sed 's/ //g')
or
comp=`echo "$fullName" | sed 's/ //g'`
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0