Test: Bash, PowerShell, Python
Operating Example How to run
System
Bash Linux #!/bin/bash chmod u+x hello.sh
Mac echo "Hello, world!" ./hello.sh
Hello, World!
Bash Variable
Here’s our birthday cake price script in Bash:
#!/bin/bash
birthdaycake=2
birthdaycake =$(( birthdaycake / 2 ))
echo The price of a birthday cake is $ birthdaycake
When we run this code, we get the following result:
The price of a birthday cake is 1
The = operator assigns a value to the variable.
The $ operator before the variable name references the value stored in that variable.
The double parenthesis (( )) tells Bash we are performing a calculation
example divide birthdaycake/2
Bash Array
dates=(1,2,5,16,23,25,28)
date [0]
The date[0] would give us the value of 1 from our array above.
#!/bin/bash
dates=(1,2,5,16,23,25,28)
echo 'The third day offering a discount is: ' ${dates[2]}
The script will produce the following output:
The third day offering a discount is 5
When referencing an array in Bash we use the {} curly braces.
Note: Bash uses zero-indexing which means when counting elements in an array, you start with
0. Therefore when dates[2] this would be the 3rd position in the array; as 1 would be in position 0,
2 would be in position 1, and 5 would be in position 2 (where our script points).