Search code examples
variablescygwinenvironment-variables

Difference between using "set" and not using set for variables? In Cygwin.


I don’t understand the difference between:

set youtube=’https://youtube.com’

and

youtube=’https://youtube.com’

With the second one, I’m able to use it in the middle of a command, such as:

cygstart $youtube 

and that works.

Why and how are these different? They both set variables?
And, when I don't use the word "set" I have to expand the variable using $? Thanks.


Solution

  • The two commands are completely unrelated; set youtube='https://youtube.com' has nothing to do with $youtube. What it does is, it sets $1 to the whole string 'youtube=https://youtube.com'.

    Per http://www.gnu.org/software/bash/manual/bashref.html#The-Set-Builtin, set is a shell builtin with three distinct purposes:

    1. If you don't give it any options or arguments, it prints out all the existing shell variables and functions.
    2. It has various options that let you change various properties of the shell. For example, set -C tells the shell that you don't want > to overwrite existing files (and that you instead want commands to fail if they would otherwise do that); and set +C tells the shell that never mind, you now want > to be able to overwrite files again.
    3. Any arguments, other than options, replace the positional parameters ($1 and $2 and so on, as well as $@ and $*).

    Since set youtube='https://youtube.com' calls set with exactly one argument, namely youtube=https://youtube.com, it has the effect of setting the first positional parameter ($1) to youtube=https://youtube.com.

    Note that youtube='https://youtube.com' is a somewhat misleading way to write youtube=https://youtube.com; the single-quotes aren't doing anything here (since the sole purpose of single-quotes is to escape whitespace and special characters, and https://youtube.com doesn't have any of these).