You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lessons/05_shell-scripts_variable.md
+44-24Lines changed: 44 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -138,13 +138,33 @@ $ num=25
138
138
139
139
Once you press return, you will find yourself back at the command prompt. **How do we know that we actually created the bash variable?**
140
140
141
-
One way to see the variable created is by using the `echo` command. As we learned earlier, this command takes the argument provided and prints it to the terminal. If we provide `num` as an argument it will be simply interpreted as a character string "num". We want `echo` to display the contents of the variable and not its name. To do this we need to **explicitly use a `$` in front of the variable name**:
141
+
One way to see the variable created is by using the `echo` command. As we learned earlier, this command takes the argument provided and prints it to the terminal. If we provide `num` as an argument it will be simply interpreted as a character string "num". We want `echo` to display the contents of the variable and not its name. To do this we need to **explicitly use a `$` in front of the variable name along with wrapping the variable in `{}`**:
142
+
143
+
```bash
144
+
$ echo${num}
145
+
```
146
+
147
+
You should see the number 25 returned to you. Did you notice that when we created the variable, there was no need for a `$`? This is standard shell notation (syntax) for defining and using variables. When defining the variable (i.e. setting the value) you can just type it as is, but when **retrieving the value of a variable don't forget the `$` and `{}`!**
148
+
149
+
It is important to note that you don't *need* to wrap your variable in `{}` and you can just return variables without them like:
142
150
143
151
```bash
144
152
$ echo$num
145
153
```
146
154
147
-
You should see the number 25 returned to you. Did you notice that when we created the variable, there was no need for a `$`? This is standard shell notation (syntax) for defining and using variables. When defining the variable (i.e. setting the value) you can just type it as is, but when **retrieving the value of a variable don't forget the `$`!**
155
+
This will work if your variable is surrounded by whitespace, or a handful of other characters, like in the above example. However, you may want to have text adjacent to your `bash` variable like:
156
+
157
+
```bash
158
+
$ echo${num}th
159
+
```
160
+
161
+
This will return the variable `num` followed by the text `th`. By providing the `{}`, we are explicitly stating the variable name to `bash` as `num`. If you don't use the `{}` in the above example:
162
+
163
+
```bash
164
+
$ echo$numth
165
+
```
166
+
167
+
Then `bash` will look for the variable `numth` and not be able to find it, so it will return nothing. In short, you will likely see many people not using `{}` around their bash variables as it can sometimes be a bit more cumbersome, however, we do encourage people to do it in order to make their variables more clearly defined.
148
168
149
169
> **NOTE:** Variables are not physical entities like files. When you create files you can use `ls` to list contents and see if the file exists. When creating variables, to list all variables in your environment you can use the command `declare` with the `-p` option. You will notice that while you only have created one variable so far, the output of `declare -p` will be more than just one variable. These other variables are called environment variables and will be [discussed in more detail later in the workshop](07_permissions_and_environment_variables.md).
150
170
>
@@ -164,13 +184,13 @@ $ file=Mov10_oe_1.subset.fq
164
184
Once you press return, you should be back at the command prompt. Let's check what's stored inside `file` using the `echo` command:
165
185
166
186
```bash
167
-
$ echo$file
187
+
$ echo${file}
168
188
```
169
189
170
190
Now let's use this variable `file` as input to one of the commands we previously learned:
171
191
172
192
```bash
173
-
$ wc -l $file
193
+
$ wc -l ${file}
174
194
```
175
195
176
196
**What do you see in the terminal? What you were expecting `wc -l` to return to you?**
@@ -180,15 +200,15 @@ The `wc -l` command is used to count and report the number of lines in a file. H
180
200
* Provide a path to the file:
181
201
182
202
```bash
183
-
$ wc -l ~/unix_lesson/raw_fastq/$file
203
+
$ wc -l ~/unix_lesson/raw_fastq/${file}
184
204
```
185
205
OR
186
206
187
207
* Change directories to where the file lives:
188
208
189
209
```bash
190
210
$ cd~/unix_lesson/raw_fastq
191
-
$ wc -l $file
211
+
$ wc -l ${file}
192
212
```
193
213
194
214
Either one of these options should have worked and you should see the number of lines in the file reported to you!
@@ -199,26 +219,26 @@ Either one of these options should have worked and you should see the number of
199
219
200
220
**Exercise**
201
221
202
-
1. Use the `$file` variable as input to the `head` and `tail` commands, and modify the arguments to display only four lines. Provide the lines of code used and report the header lines (`@HWI`) you retrieve from each command.
222
+
1. Use the `${file}` variable as input to the `head` and `tail` commands, and modify the arguments to display only four lines. Provide the lines of code used and report the header lines (`@HWI`) you retrieve from each command.
203
223
2. Create a new variable called `meta` and assign it the value `Mov10_rnaseq_metadata.txt`. For the following questions, use the `$meta` variable but do not change directories. Provide the code you would run to:
204
224
1. Display the contents of the file using `cat`.
205
225
2. Retrieve only the lines which contain normal samples. (*Hint: use `grep`*).
206
226
207
227
<details>
208
228
<summary><b><i>Answers</i></b></summary>
209
229
<p><i>Question 1</i><br>
210
-
<code>head -n 4 $file</code><br>
230
+
<code>head -n 4 ${file}</code><br>
211
231
@HWI-ST330:304:H045HADXX:1:1101:1162:205<br></p>
212
-
<p><code>tail -n 4 $file</code><br>
232
+
<p><code>tail -n 4 ${file}</code><br>
213
233
@HWI-ST330:304:H045HADXX:2:2212:15724:100530</p>
214
234
<i>Question 2</i><br>
215
235
<code>meta=Mov10_rnaseq_metadata.txt</code>
216
236
<p><i>Part i</i><br>
217
-
<code>cat ../other/$meta</code> (relative path) or <br>
Once you press return you should be back at the command prompt. Check to see what got stored in the `samplename` variable:
285
305
286
306
```bash
287
-
$ echo$samplename
307
+
$ echo${samplename}
288
308
```
289
309
290
310
> #### The `basename` command
@@ -324,25 +344,25 @@ Next, we will create another variable to store the directory name. To get the di
324
344
325
345
```bash
326
346
# Get only the directory name
327
-
dirName=`basename $dirPath`
347
+
dirName=`basename ${dirPath}`
328
348
```
329
349
330
350
The next few tasks we want to execute require simple commands for changing directories (`cd`), listing contents of a directory (`ls -l`). We can add these into our script making sure we are referencing the correct variable and also including meaningful `echo` statements for verbosity.
331
351
332
352
```bash
333
-
echo"Reporting on the directory"$dirName"..."
353
+
echo"Reporting on the directory"${dirName}"..."
334
354
335
355
# Move into the directory
336
-
cd$dirPath
356
+
cd${dirPath}
337
357
338
-
echo"These are the contents of"$dirName
358
+
echo"These are the contents of"${dirName}
339
359
ls -l
340
360
```
341
361
342
362
The final task of reporting the total number of files will require us to pipe (`|`) together multiple commands:
343
363
344
364
```bash
345
-
echo"The total number of files contained in"$dirName
365
+
echo"The total number of files contained in"${dirName}
346
366
ls | wc -l
347
367
348
368
echo"Report complete!"
@@ -355,17 +375,17 @@ After adding in a final `echo` statement, we are all set with script! Make sure
355
375
dirPath=~/unix_lesson/raw_fastq
356
376
357
377
# Get only the directory name
358
-
dirName=`basename $dirPath`
378
+
dirName=`basename ${dirPath}`
359
379
360
-
echo"Reporting on the directory"$dirName"..."
380
+
echo"Reporting on the directory"${dirName}"..."
361
381
362
382
# Move into the directory
363
-
cd$dirPath
383
+
cd${dirPath}
364
384
365
-
echo"These are the contents of"$dirName
385
+
echo"These are the contents of"${dirName}
366
386
ls -l
367
387
368
-
echo"The total number of files contained in"$dirName
388
+
echo"The total number of files contained in"${dirName}
0 commit comments