I have created following file with name test.sh


name = "Elvin John Paul"
echo $name
echo ${name#* }
echo ${name##* }
echo ${name% *}
echo ${name%% *}

and I am getting some error as below. enter image description here

can anyone tell me what wrong I am doing?


There are two things wrong:

  • You cannot use spaces around the assignment operator. As your script currently is, bash interprets the line to mean "run name with the arguments = and Elvin John Paul". The line should look like the following:
name="Elvin John Paul"
  • You have Windows-style line endings (CRLF instead of just LF), which results in the error about $'\r'. In recent Cygwin versions, you can use set -o igncr to ignore carriage returns as part of a CR LF, or you can fix this with sed like so:
sed 's/\r$//' file > file2

Please remove the space on either side of the = symbol. Unlike many other programming languages, that space around the assignment operator matters in Bash and other shell scripts.

The correct code should be:

name="Elvin John Paul"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.