2

I thought there is no syntax for echo keyword i.e we can use " or ' or no quotes.

I tried all three types in bash shell

I got the error. Why it is happening?

{#Azar's first shell script
echo "Hi Azar, Welcome to the new world of technology for you"
echo 'default script bash'
echo keep going don't stop always}

error:

 sh afs.sh
 Hi Azar, Welcome to the new world of technology for you
 default script bash
 afs.sh: 6: afs.sh: Syntax error: Unterminated quoted string
1
  • 1
    It's not echo syntax, rather it's bash preprocessing line before execution. – alamar Feb 26 '17 at 8:28
10

Your script fails when you are trying to

echo keep going don't stop always

The reason is told in the syntax error

Unterminated quoted string

Because you use ' in don't, Bash shell keeps looking for another ' that closes your sentence. As it is never found, syntax error is printed.

In this case, you need to use double quotes (" ") like this

echo "keep going don't stop always"

It is recommended to use quotes when printing out something in general. In double quotes you can use ' and in single ones " if needed.

0
7

In addition to the answer provided by jiipeezz to this question, you can also use backslash to escape single characters.

echo keep going don\'t stop always

For more details read this.

1
  • 1
    Good point, but it is always a good practice to wrap your strings around quotes. – Wtower Feb 26 '17 at 10:47
0

Your script looks erroneous. I tried the same, but got a command not found error.

line 3: $'{#Azars first shell script\necho "Hi Azar, Welcome to the new world of technology for you"\necho default': command not found

This script worked without any errors as expected. Escape the quote you have used in "don't" --- "don\'t". This way you do not have to put double quotes around echo statement. But it is a good practice to put them into double quotes.

#Azar's first shell script
echo "Hi Azar, Welcome to the new world of technology for you"
echo 'default script bash' 
echo keep going don\'t stop always

Output of this script.

user@system:~/samplecode$ bash samplescript.sh
Hi Azar, Welcome to the new world of technology for you
default script bash
keep going don't stop always

Happy scripting.

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.