Ask Your Question
1

Difference between ' ' and " " in bash scripting language

asked 2016-01-07 14:10:18 -0600

Andrei Costache gravatar image

Hello guys,

I started studying a bit of bash scripting language, and I am not able really to understand what is the difference between using e.g.

echo "this is a string"

and using

echo 'this is a string'

They seem to be interchangeable in this simple example, but I have a strong feeling that they have different uses when it comes to more complicated topics such as variables, evaluations of expressions, usage of bash functions...

Can anyone explain to me what is the difference?

Thanks

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-01-07 15:16:23 -0600

sideburns gravatar image

Welcome to ask.fedora. Yes, there are differences, depending on how and where you use them. I'm no expert on such things, so instead of trying to answer your question directly, and probably getting it wrong, I'll send you to this Guide to Advanced Bash Scripting that probably has more info than you need right now. I hope it's as useful to you as it has been to me on several occasions. Please feel free to ask more questions, because that's what we're here for.

edit flag offensive delete link more

Comments

Thanks a lot for the link. While it is indeed much more detailed than a new user would need it to be in order to get the basics, it has very good information inside and has already answered some other questions I had about the "Quoting" topic.

To anyone interested, the section "5 Quoting" has some examples of quoting and clear explanations (and also some complex examples for the advanced among us with not so clear explanations; but I'm sure it's because I am not among the advanced :D)

Andrei Costache gravatar imageAndrei Costache ( 2016-01-07 15:33:35 -0600 )edit
2

answered 2016-01-07 15:40:10 -0600

Andrei Costache gravatar image

To make it simple:

We use single quotes (') to preserve the literal meaning of the string; that is: no special characters inside the string will be interpreted as special. - exception is the actual ' character which needs to be escaped if it is present in the string (examples are available in section 5.1 of the above guide)

We use double quotes (") to allow the interpreter to interpret special characters inside the string as special. Examples of special characters:

  • $ - references a variable value
  • () and ` - used to tell the interpreter to execute a function/command,
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-01-07 14:10:18 -0600

Seen: 104 times

Last updated: Jan 07 '16