Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using double quotes in bash export statement

Tags:

bash

Hello I'm reading a book about bash scripting and the author says to add the following to the end of my .bashrc file. export PATH=~/bin:"$PATH" in order to execute my file from the command line by typing its name. I notice however that if I put export PATH=~/bin:$PATH I can achieve the same result. So my question is what is the difference between the one with quotes and the one without quotes? thanks.

like image 463
Jordan Camp Avatar asked Dec 05 '14 03:12

Jordan Camp


3 Answers

I was facing the same with trying to assign a JSON string to a variable in the terminal.

Wrap it with Single Quotes or Double Quotes

Use single quotes, if you string contains double quotes and vice-versa.

$ export TEMP_ENV='I like the "London" bridge'
$ echo $TEMP_ENV
>> I like the "London" bridge


$ export TEMP_ENV="I like the 'London' bridge"
$ echo $TEMP_ENV
>> I like the 'London' bridge
like image 188
Keet Sugathadasa Avatar answered Nov 15 '22 20:11

Keet Sugathadasa


The quotes won't hurt anything, but neither are they necessary. Assignments are processed specially by the shell. From the man page:

A variable may be assigned to by a statement of the form

          name=[value]

If value is not given, the variable is assigned the null string. All values undergo tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal (see EXPANSION below).

Notice that word-splitting and pathname generation are not on the list in bold. These are the two types of expansion you are trying to prevent by quoting a parameter expansion, but in this context they are not performed. The same rules apply to the assignments that are passed to the export built-in command.

like image 32
chepner Avatar answered Nov 15 '22 20:11

chepner


You must include the variable PATH inside double quotes. So that it would handle the filepaths which has spaces but without double quotes, it won't handle the filenames which has spaces in it.

like image 4
Avinash Raj Avatar answered Nov 15 '22 22:11

Avinash Raj