Skip to content

Commit 56cdca1

Browse files
authored
Updated bash variables on {}
1 parent 01717c5 commit 56cdca1

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

lessons/05_shell-scripts_variable.md

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,33 @@ $ num=25
138138

139139
Once you press return, you will find yourself back at the command prompt. **How do we know that we actually created the bash variable?**
140140

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:
142150

143151
```bash
144152
$ echo $num
145153
```
146154

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.
148168

149169
> **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).
150170
>
@@ -164,13 +184,13 @@ $ file=Mov10_oe_1.subset.fq
164184
Once you press return, you should be back at the command prompt. Let's check what's stored inside `file` using the `echo` command:
165185

166186
```bash
167-
$ echo $file
187+
$ echo ${file}
168188
```
169189

170190
Now let's use this variable `file` as input to one of the commands we previously learned:
171191

172192
```bash
173-
$ wc -l $file
193+
$ wc -l ${file}
174194
```
175195

176196
**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
180200
* Provide a path to the file:
181201

182202
```bash
183-
$ wc -l ~/unix_lesson/raw_fastq/$file
203+
$ wc -l ~/unix_lesson/raw_fastq/${file}
184204
```
185205
OR
186206

187207
* Change directories to where the file lives:
188208

189209
```bash
190210
$ cd ~/unix_lesson/raw_fastq
191-
$ wc -l $file
211+
$ wc -l ${file}
192212
```
193213

194214
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
199219

200220
**Exercise**
201221

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.
203223
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:
204224
1. Display the contents of the file using `cat`.
205225
2. Retrieve only the lines which contain normal samples. (*Hint: use `grep`*).
206226

207227
<details>
208228
<summary><b><i>Answers</i></b></summary>
209229
<p><i>Question 1</i><br>
210-
<code>head -n 4 $file</code><br>
230+
<code>head -n 4 ${file}</code><br>
211231
@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>
213233
@HWI-ST330:304:H045HADXX:2:2212:15724:100530</p>
214234
<i>Question 2</i><br>
215235
<code>meta=Mov10_rnaseq_metadata.txt</code>
216236
<p><i>Part i</i><br>
217-
<code>cat ../other/$meta</code> (relative path) or <br>
218-
<code>cat ~/unix_lesson/other/$meta</code> (full path)<br></p>
237+
<code>cat ../other/${meta}</code> (relative path) or <br>
238+
<code>cat ~/unix_lesson/other/${meta}</code> (full path)<br></p>
219239
<p><i>Part ii</i><br>
220-
<code>grep normal ../other/$meta</code> (relative path) or<br>
221-
<code>grep normal ~/unix_lesson/other/$meta</code> (full path)<br></p>
240+
<code>grep normal ../other/${meta}</code> (relative path) or<br>
241+
<code>grep normal ~/unix_lesson/other/${meta}</code> (full path)<br></p>
222242
</details>
223243

224244

@@ -284,7 +304,7 @@ $ samplename=`basename ~/unix_lesson/raw_fastq/Mov10_oe_1.subset.fq .fq`
284304
Once you press return you should be back at the command prompt. Check to see what got stored in the `samplename` variable:
285305

286306
```bash
287-
$ echo $samplename
307+
$ echo ${samplename}
288308
```
289309

290310
> #### The `basename` command
@@ -324,25 +344,25 @@ Next, we will create another variable to store the directory name. To get the di
324344

325345
```bash
326346
# Get only the directory name
327-
dirName=`basename $dirPath`
347+
dirName=`basename ${dirPath}`
328348
```
329349

330350
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.
331351

332352
```bash
333-
echo "Reporting on the directory" $dirName "..."
353+
echo "Reporting on the directory" ${dirName} "..."
334354

335355
# Move into the directory
336-
cd $dirPath
356+
cd ${dirPath}
337357

338-
echo "These are the contents of" $dirName
358+
echo "These are the contents of" ${dirName}
339359
ls -l
340360
```
341361

342362
The final task of reporting the total number of files will require us to pipe (`|`) together multiple commands:
343363

344364
```bash
345-
echo "The total number of files contained in" $dirName
365+
echo "The total number of files contained in" ${dirName}
346366
ls | wc -l
347367

348368
echo "Report complete!"
@@ -355,17 +375,17 @@ After adding in a final `echo` statement, we are all set with script! Make sure
355375
dirPath=~/unix_lesson/raw_fastq
356376

357377
# Get only the directory name
358-
dirName=`basename $dirPath`
378+
dirName=`basename ${dirPath}`
359379

360-
echo "Reporting on the directory" $dirName "..."
380+
echo "Reporting on the directory" ${dirName} "..."
361381

362382
# Move into the directory
363-
cd $dirPath
383+
cd ${dirPath}
364384

365-
echo "These are the contents of" $dirName
385+
echo "These are the contents of" ${dirName}
366386
ls -l
367387

368-
echo "The total number of files contained in" $dirName
388+
echo "The total number of files contained in" ${dirName}
369389
ls | wc -l
370390

371391
echo "Report complete!"

0 commit comments

Comments
 (0)