Missing Assigned Variable within logic operator

Hey ,

I'm trying to perform the following command, however it cannot read the variable assigned earlier. I'm not sure why this happen. Please help thanks

while :
do
echo "what's ur name? (if none just press [ENTER])"
read name
changeName = echo $name | sed "s/on/ey/"
echo $changeName  #this variable still can be read
if [ -z $name ]; then
 echo "you don't have a name goodbye"
 exit 0
else
 echo "your name will change from: $name to $changeName" #now changeName cannot be read why?
fi
done
 

To assign a command output to the variable you need to use the backtick(`).and also you should not give space while assign the output to a variable.

while :
do
echo "what's ur name? (if none just press [ENTER])"
read name
changeName=`echo $name | sed "s/on/ey/"`
echo $changeName  #this variable still can be read
if [ -z $name ]; then
 echo "you don't have a name goodbye"
 exit 0
else
 echo "your name will change from: $name to $changeName" #now changeName cannot be read why?
fi
done

Hey,

Thanks for replying but I've tried using the back tick and i didn't give a space when assigning variable but instead i'm getting this error

./testVar: line 7: jey: command not found

the changeName=`echo $name | sed "s/on/ey/"` command cannot be read if i used the back tick

please helpp

changeName=`echo "$name" | sed "s/on/ey/"`
if [ -z "$name" ]; then

Use double quotes for the variable $name. Because your input may have space characters.
Then only it will preserve the white spaces

I thing you have gave space characters in your input , to avoid this use the double quotes.

while :
do
echo "what's ur name? (if none just press [ENTER])"
read name
changeName=`echo "$name" | sed "s/on/ey/"`
echo $changeName  #this variable still can be read
if [ -z "$name" ]; then
 echo "you don't have a name goodbye"
 exit 0
else
 echo "your name will change from: $name to $changeName" #now changeName cannot be read why?
fi
done

Hey Guys,

thanks for the fast replies, even though i used double quotes it still doesn't wor and my input doesn't have any white spaces

here is the code suggested

#!/bin/sh

while :
do
echo "what's ur name? (if none just press [ENTER])"
read name
changeName= `echo "$name" | sed "s/on/ey/"`
echo $changeName  #this variable still can be read
if [ -z "$name" ]; then
 echo "you don't have a name goodbye"
 exit 0
else
 echo "your name will change from: $name to "$changeName" ? [y/n]"
 read ans
fi
done




This is the ouput I got:

what's ur name? (if none just press [ENTER])
jon
./testVar: line 7: jey: command not found

your name will change from: jon to ? [y/n]

I'm confused never had this problem before

#!/bin/sh

while :
do
echo "what's ur name? (if none just press [ENTER])"
read name
changeName=`echo "$name" | sed "s/on/ey/"`
echo $changeName  #this variable still can be read
if [ -z "$name" ]; then
 echo "you don't have a name goodbye"
 exit 0
else
 echo "your name will change from: $name to "$changeName" ? [y/n]"
 read ans
fi
done

I just removed the space after the assignment
operator as in the below line and it is working for me...

changeName=`echo "$name" | sed "s/on/ey/"`

And it will keep on looping till you don't put a name...

OMG that was stupid of mee....Thanks a lot guys it's working now!!

Modern practice is to use '[[...]]' instead of '[...]' and '$(...)' instead of `...`

#!/usr/bin/bash

while read -p "what's ur name? (if none just press [ENTER])" name
do
   if [[ -z "$name" ]]; then
      echo "you don't have a name goodbye"
      exit 0
   fi
   changeName=$(echo $name | sed "s/on/ey/")
   echo "your name will change from: $name to $changeName"
done