What is the difference between the following 2 examples in a shell script:
set variable1 "value"
set variable2 value
What is the difference between the following 2 examples in a shell script:
set variable1 "value"
set variable2 value
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 quoting 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 (), but you can set all the positional parameters by running 1=foo
set
with parameters. For example:
#!/bin/sh
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.
variable1
to$1
, whilst the second will assignvariable2
to$1
. Both will assignvalue
to$2
. HTH. – Toby Speight Jun 15 '16 at 12:28"$@"
will expand tovariable1
value
, but after the second, it will expand tovariable2
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 ofset
in the section Shell Builtin Commands. – Toby Speight Jun 15 '16 at 12:54set variable value
does not assignvalue
tovariable
. – Gilles 'SO- stop being evil' Jun 15 '16 at 22:00