This is sometimes referred to as expanding the variable, or parameter substitution: var_a = "Hello World" another_var echo $var_a Whenever Bash encounters a dollar-sign, immediately followed by a word, within a command or in a double-quoted string, it will attempt to replace that token with the value of the named variable. Var_a = "Hello World" Referencing the value of a variable In particular, no whitespace is allowed between the variable name, the equals sign, and the value.Īll of these examples would cause Bash to throw an error: var_a = "Hello World" Unlike most modern languages, Bash is pretty picky about the syntax for setting variables. The following command assigns Hello World to the variable named var_a, and 42 to another_var var_a = "Hello World" another_var =42 The following routine reads each line (via cat, which isn't best practice…but will do for now) into a for loop, which then downloads each URL: for url in $(cat websites.txt ) doĭone Variables Basic variable usage and syntax Setting a variable A variable acts as a placeholder that gets resolved upon actual execution time.įor example, imagine that websites.txt contains a list of website addresses. However, variables really come into use in more advanced programming, when we're in a situation in which the actual values aren't known before executing a program. Variables can be used, at the very least, to make code more readable for humans: domain = '' path = '/some/path' base_url = " $domain$path " page = 'index.html' # download # and save to 'downloads/index.html'Ĭurl " $base_url / $page " -o "downloads/ $page " This is commonly referred to as variables. Using variables to refer to data, including the results of a command.Īn essential feature of programming is the ability to use a name or a label to refer to some other quantity: such as a value, or a command.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |