Skip to content

Commit 7a580e7

Browse files
committed
Add Check If The First Argument Is Given as a shell til
1 parent 04ab027 commit 7a580e7

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
99

1010
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1111

12-
_950 TILs and counting..._
12+
_951 TILs and counting..._
1313

1414
---
1515

@@ -41,6 +41,7 @@ _950 TILs and counting..._
4141
* [React Testing Library](#react-testing-library)
4242
* [ReasonML](#reasonml)
4343
* [Ruby](#ruby)
44+
* [Shell](#shell)
4445
* [tmux](#tmux)
4546
* [Unix](#unix)
4647
* [Vim](#vim)
@@ -819,6 +820,10 @@ _950 TILs and counting..._
819820
- [Wrap Things In An Array, Even Hashes](ruby/wrap-things-in-an-array-even-hashes.md)
820821
- [Zero Padding](ruby/zero-padding.md)
821822

823+
### Shell
824+
825+
- [Check If The First Argument Is Given](shell/check-if-the-first-argument-is-given.md)
826+
822827
### tmux
823828

824829
- [Adjusting Window Pane Size](tmux/adjusting-window-pane-size.md)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Check If The First Argument Is Given
2+
3+
In a shell script, you may want to check if an argument was given. Each
4+
argument is referenced numerically with the `$` prefix, so the first argument
5+
is `$1`. To check if the first argument is given, you can use the `-z` check.
6+
7+
```bash
8+
if [ -z "$1" ]
9+
then
10+
echo "The first argument is missing"
11+
exit 1
12+
fi
13+
```
14+
15+
The `-z` checks if the argument is a zero-length string (so `""` or undefined
16+
will be true). If it is missing, then we echo out a message and exit the
17+
script. This is how I might fashion a script that requires the first argument.
18+
19+
[source](https://stackoverflow.com/questions/6482377/check-existence-of-input-argument-in-a-bash-shell-script)

0 commit comments

Comments
 (0)