What is the difference between the following 2 examples in a shell script:

set variable1             "value"

set variable2              value
  • 3
    Bash documentation covers quoting rather extensively: gnu.org/software/bash/manual/bashref.html#Quoting
    – phemmer
    Jun 15 '16 at 12:16
  • 2
    The first will assign variable1 to $1, whilst the second will assign variable2 to $1. Both will assign value to $2. HTH. Jun 15 '16 at 12:28
  • 1
    @TobySpeight Could you please make your statement a bit clear? Jun 15 '16 at 12:40
  • They both set the positional parameters. After executing the first, "$@" will expand to variable1 value, but after the second, it will expand to variable2 value. The difference is at the end of the first positional parameter. In the Bash man page, you should be reading the section Positional Parameters, and the description of set in the section Shell Builtin Commands. Jun 15 '16 at 12:54
  • Okay, I still do not see you point, But this is what I found after some more research The characters with special meaning to shell exp & < etc. cause termination of a pattern if not quoted. The quotes preserve the literal value of the pattern within the quote Jun 15 '16 at 13:13

In a shell script, value and "value" and 'value' are all equivalent. You can choose to use quotes or not. The reason quotes exist is that sometimes, you want to put a character with a special meaning in a string, and then the quotes let the shell know that the special character is part of the string. For example:

var=value more
var='value more'
var="value more"
var=value\ more

The first line assigns the value value (a 5-character string) to the variable whose name is var; more is a separate word of this command, since there's a space before it, and the effect of the space is that the command runs the program more with the environment variable var set to value. The other three lines all have the same meaning: they just set the variable var to the value value more (a 10-character string, where the 6th character is a string). Single quotes '…' and "…" surround a string. The third possibility for quoting is the backslash, which quotes only the next character.

The difference between single quotes and double quotes is that between single quotes, every character stands for itself, but between double quotes, a few characters are still interpreted. In particular, "\\" is a string containing a single backslash, because the backlash in the string is interpreted as quoting the next character, and likewise "\"" is a string containing just a double quote; "\$var" is a 4-character string beginning with a dollar sign, whereas "$var" is a string which is the value of the variable var. In contrast, '$var' is a 4-character string beginning with a dollar sign and '\$var' is a 5-character string beginning with a backslash.

There are more subtleties to do with quoting, and I won't go into them here; read a book or a manual (e.g. the bash manual) for details, and perhaps browse the tag on this site.

Note that the set command does not assign a value to a variable. set variable value does not assign a value to the variable variable, the syntax for that would be variable=value. What the set command does is to replace the parameters of the script (or function). When you run a script with arguments, these are available in the script in special variables that have a number rather than a name: $1, $2, …. Those variables are called positional parameters (parameters because they're the parameters of the script/function, positional because they're accessed by their position in the list of parameters). You can't assign to those variables using the number (1=foo), but you can set all the positional parameters by running set with parameters. For example:

echo "$1"
set hello world
echo "$1"

This script prints the first argument that was passed on its command line, then prints hello. The set builtin has other jobs, see the manual for more details.

For basic shell programming, you can forget about this use of set for now. The basics of quoting are important. One thing you do need to know is to always use double quotes around variable expansions, i.e. "$foo" and not plain $foo, because plain $foo mangles the value.

Everything I wrote in this answer is about Bourne/POSIX-style shells: sh, dash, bash, ksh, zsh, etc. There are other, less common shell families with different syntax. In (t)csh, set is involved in some assignments, but an equal sign is still involved; quoting with \'" is somewhat similar but not identical. In fish, set variable value does set the variable variable to the value value; there too quoting with \'" is somewhat similar but not identical.

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.