15

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

1 Answer 1

27

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'`
1
  • 5
    It should perhaps be noted that the $(..) syntax is preferred to the older `..` syntax for reasons of readability and nesting.
    – evilsoup
    Oct 5, 2013 at 14:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .