File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
10
10
11
11
For a steady stream of TILs, [ sign up for my newsletter] ( https://tinyletter.com/jbranchaud ) .
12
12
13
- _ 1017 TILs and counting..._
13
+ _ 1018 TILs and counting..._
14
14
15
15
---
16
16
@@ -932,6 +932,7 @@ _1017 TILs and counting..._
932
932
- [ Check The Current Working Directory] ( unix/check-the-current-working-directory.md )
933
933
- [ Clear The Screen] ( unix/clear-the-screen.md )
934
934
- [ Command Line Length Limitations] ( unix/command-line-length-limitations.md )
935
+ - [ Compare Two Variables In A Bash Script] ( unix/compare-two-variables-in-a-bash-script.md )
935
936
- [ Configure cd To Behave Like pushd In Zsh] ( unix/configure-cd-to-behave-like-pushd-in-zsh.md )
936
937
- [ Copying File Contents To System Paste Buffer] ( unix/copying-file-contents-to-system-paste-buffer.md )
937
938
- [ Copying Nested Directories With Ditto] ( unix/copying-nested-directories-with-ditto.md )
Original file line number Diff line number Diff line change
1
+ # Compare Two Variables In A Bash Script
2
+
3
+ You can compare two variables in a bash script with an ` if ` block like so:
4
+
5
+ ``` bash
6
+ if [ " $EDITOR " = " $PREFERRED_EDITOR " ]; then
7
+ # do something ...
8
+ fi
9
+ ```
10
+
11
+ If those variables are equal, then the contents of the ` if ` block will be
12
+ executed.
13
+
14
+ Notice that both variables are wrapped in quotes. This is to avoid a potential
15
+ syntax error. If the quotes were excluded and one of the variables happened to
16
+ be unset, then the comparison would evaluate to:
17
+
18
+ ``` bash
19
+ if [ " vim" = ]; then
20
+ # do something ...
21
+ fi
22
+ ```
23
+
24
+ That would cause an error, rather than evaluating to false and moving in.
25
+ Wrapping each in quotes allows an unset variable to turn into an empty string
26
+ (` "" ` ).
You can’t perform that action at this time.
0 commit comments