-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbash_variables_two
More file actions
executable file
·47 lines (32 loc) · 1.55 KB
/
bash_variables_two
File metadata and controls
executable file
·47 lines (32 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env bash
VAR_ONE="value\n" #
VAR_TWO='value\n'
VAR_THREE=value\\n
# command output can be piped to an inline string,
# just like a declared variable using $( ... )
# this line will inject the output of the printf command into the string.
echo This is VAR_ONE: $( printf $VAR_ONE ), VAR_TWO: $( printf $VAR_TWO ), VAR_THREE: $( printf $VAR_THREE )
echo # -------------------------------
# the output can also be assigned to another variable.
BASH_BIN=$( which bash )
# the which command returns the whole path for the given executable.
echo the bash binary is located at $BASH_BIN
echo the bash binary is located at $( which bash ) # same output as above.
# you can even embed a command inside of another command.
echo the bash binary is located at $( dirname $( which bash ) )
echo # -------------------------------
# Mathematical calculations can be performed as well using
# the $(( ... )) wrappers. Note that arbitrary spaces can be
# added within the parentheses.
NUMBER_TWO=$(( 1 + 1 )) # addition
NUMBER_SIX=$(( $NUMBER_TWO * 3 )) # multiplication
NUMBER_THREE=$(( $NUMBER_SIX / 2 )) # division
NUMBER_FOUR=$(( $NUMBER_SIX - $NUMBER_TWO )) # subtraction
NUMBER_ONE=$(( $NUMBER_FOUR % $NUMBER_THREE )) # modulo
echo The Number Two: $NUMBER_TWO
echo The Number Six: $NUMBER_SIX
echo The Number Three: $NUMBER_THREE
echo The Number Four: $NUMBER_FOUR
echo The Number One: $NUMBER_ONE
# Note that Bash cannot perform floating point calculations
# natively. It is limited to only integer calculations.