github.com/NeowayLabs/nash@v0.2.2-0.20200127205349-a227041ffd50/docs/reference.md (about) 1 <!-- mdtocstart --> 2 3 # Table of Contents 4 5 - [Command line arguments](#command-line-arguments) 6 - [Flow control](#flow-control) 7 - [Branching](#branching) 8 - [Looping](#looping) 9 - [Lists](#lists) 10 - [Forever](#forever) 11 - [Functions](#functions) 12 - [Operators](#operators) 13 - [+](#) 14 - [string](#string) 15 - [Packages](#packages) 16 - [Iterating](#iterating) 17 - [Built-in functions](#builtin-functions) 18 - [print](#print) 19 - [format](#format) 20 - [len](#len) 21 - [append](#append) 22 - [exit](#exit) 23 - [glob](#glob) 24 - [Standard Library](#standard-library) 25 26 <!-- mdtocend --> 27 28 Here lies a comprehensive reference documentation of nash 29 features and built-in functions, and how to use them. 30 31 There is also some [examples](./examples) that can be useful. 32 33 34 # Command line arguments 35 36 To handle script arguments you can just use the ARGS variable, 37 that is a list populated with the arguments passed to your script 38 when it is executed, like: 39 40 ```nash 41 echo 42 echo "acessing individual parameter" 43 var somearg = $ARGS[0] 44 echo $somearg 45 echo 46 ``` 47 48 # Flow control 49 50 ## Branching 51 52 To branch you can use **if** statement, it requires 53 a boolean expression, like the comparison operator: 54 55 ```nash 56 var a = "nash" 57 echo -n $a 58 if $a == "nash" { 59 a = "rocks" 60 } 61 echo $a 62 #Output:"nashrocks" 63 ``` 64 65 You can also use a junction of boolean expressions: 66 67 ```nash 68 a = "nash" 69 b = "rocks" 70 if $a == "nash" && $b == "rocks"{ 71 echo "hellyeah" 72 } 73 #Output:"hellyeah" 74 ``` 75 76 You can also use a disjunction of boolean expressions: 77 78 ```nash 79 a = "nash" 80 b = "rocks" 81 if $a == "bash" || $b == "rocks"{ 82 echo "hellyeah" 83 } 84 #Output:"hellyeah" 85 ``` 86 87 ## Looping 88 89 Right now there are two kind of loops, on lists 90 and the forever kind :-). 91 92 ### Lists 93 94 You can iterate lists like this: 95 96 ```nash 97 a = "" 98 for i in ("nash" "rocks"){ 99 a = $a + $i 100 } 101 echo $a 102 #Output:"nashrocks" 103 ``` 104 105 ### Forever 106 107 It would be cool to loop on boolean expressions, but 108 right now we can only loop forever (besides list 109 looping): 110 111 ```nash 112 for { 113 echo "hi" 114 } 115 ``` 116 117 # Functions 118 119 Defining functions is very easy, for example: 120 121 ```nash 122 fn concat(a, b) { 123 return $a+$b 124 } 125 126 res <= concat("1","9") 127 echo $res 128 129 #Output:"19" 130 ``` 131 132 If a parameter is missing on the function call, 133 it will fail: 134 135 ```nash 136 fn concat(a, b) { 137 return $a, $b 138 } 139 140 res <= concat("1") 141 echo $res 142 143 #Output:"ERROR: Wrong number of arguments for function concat. Expected 2 but found 1" 144 ``` 145 146 Passing extra parameters will also fail: 147 148 ```nash 149 fn concat(a, b) { 150 return $a, $b 151 } 152 153 res <= concat("1","2","3") 154 echo $res 155 156 #Output:"ERROR: Wrong number of arguments for function concat. Expected 2 but found 3" 157 ``` 158 159 # Operators 160 161 ## + 162 163 The **+** operator behaviour 164 is dependent on the type its operands. It 165 is always invalid to mix types on the operation 166 (like one operand is a string and the other one is a integer). 167 168 The language is dynamically typed, but it is strongly 169 typed, types can't be mixed on operations, there is no 170 implicit type coercion. 171 172 ### string 173 174 String concatenation is pretty straightforward. 175 For example: 176 177 ```nash 178 a = "1" 179 b = "2" 180 181 echo $a+$b 182 #Output:"12" 183 ``` 184 185 # Packages 186 187 TODO 188 189 # Iterating 190 191 TODO 192 193 # Built-in functions 194 195 Built-in functions are functions that are embedded on the 196 language. You do not have to import any package to use them. 197 198 ## print 199 200 The function **print** is used to print simple 201 messages directly to stdout: 202 203 ```nash 204 print("hi") 205 #Output:"hi" 206 ``` 207 208 And supports formatting: 209 210 ```nash 211 print("%s:%s", "1", "2") 212 #Output:"1:2" 213 ``` 214 215 ## format 216 217 The function **format** is used like **print**, but 218 instead of writing to stdout it will return the string 219 according to the format provided: 220 221 ```nash 222 a <= format("%s:%s", "1", "2") 223 echo $a 224 #Output:"1:2" 225 ``` 226 227 ## len 228 229 The function **len** returns the length of a list. 230 An example to check for the length of a list: 231 232 ``` 233 echo "define one list with two elements" 234 args = ( 235 "one" 236 "two" 237 ) 238 echo "getting list length" 239 argslen <= len($args) 240 echo $argslen 241 ``` 242 243 ## append 244 245 The function **append** appends one element to the end of a exist list. 246 Append returns the updated list. 247 248 An example to append one element to a exist list: 249 250 ``` 251 example_list = () 252 echo "appending string 1" 253 example_list <= append($example_list, "1") 254 echo $example_list 255 echo "appending string 2" 256 example_list <= append($example_list, "2") 257 echo $example_list 258 ``` 259 260 ## exit 261 262 TODO 263 264 ## glob 265 266 TODO 267 268 # Standard Library 269 270 The standard library is a set of packages that comes with the 271 nash install (although not obligatory). 272 273 They must be imported explicitly (as any other package) to 274 be used. 275 276 * [fmt](docs/stdlib/fmt.md)