Skip to content

Commit 5b17b2f

Browse files
authored
Bash var doc update (bobbyiliev#113)
* Update 004-bash-variables.md Another Hacktober Fest PR! I think that inputs in the commandline should also be mentioned! * Update 004-bash-variables.md Hello again ^^ another Hacktober Fest PR! I think that reading inputs in the Command Line should also be mentioned! * Update 004-bash-variables.md Hello again ^^ another Hacktober Fest PR! I think that reading inputs in the Command Line should also be mentioned!
1 parent 33ad0e3 commit 5b17b2f

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

ebook/en/content/004-bash-variables.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,52 @@ You would see the following output on your screen:
8282
```bash
8383
Hello DevDojo
8484
```
85-
8685
Note that you don't necessarily need to add semicolon `;` at the end of each line. It works both ways, a bit like other programming language such as JavaScript!
86+
87+
88+
You can also add variables in the Command Line outside the Bash script and they can be read as parameters:
89+
90+
```bash
91+
./devdojo.sh Bobby buddy!
92+
```
93+
This script takes in two parameters `Bobby`and `buddy!` seperated by space. In the `devdojo.sh` file we have the following:
94+
95+
```bash
96+
#!/bin/bash
97+
98+
echo "Hello there" $1
99+
100+
```
101+
`$1` is the first input (`Bobby`) in the Command Line. Similarly, there could be more inputs and they are all referenced to by the `$` sign and their respective order of input. This means that `buddy!` is referenced to using `$2`. Another useful method for reading variables is the `$@` which reads all inputs.
102+
103+
So now let's change the `devdojo.sh` file to better understand:
104+
105+
```bash
106+
#!/bin/bash
107+
108+
echo "Hello there" $1
109+
110+
# $1 : first parameter
111+
112+
echo "Hello there" $2
113+
114+
# $2 : second parameter
115+
116+
echo "Hello there" $@
117+
118+
# $@ : all
119+
```
120+
The ouput for:
121+
122+
```bash
123+
./devdojo.sh Bobby buddy!
124+
```
125+
Would be the following:
126+
127+
```bash
128+
Hello there Bobby
129+
Hello there buddy!
130+
Hello there Bobby buddy!
131+
```
132+
133+

0 commit comments

Comments
 (0)