diff --git a/README.md b/README.md index ea25afe..d4494aa 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_950 TILs and counting..._ +_951 TILs and counting..._ --- @@ -41,6 +41,7 @@ _950 TILs and counting..._ * [React Testing Library](#react-testing-library) * [ReasonML](#reasonml) * [Ruby](#ruby) +* [Shell](#shell) * [tmux](#tmux) * [Unix](#unix) * [Vim](#vim) @@ -819,6 +820,10 @@ _950 TILs and counting..._ - [Wrap Things In An Array, Even Hashes](ruby/wrap-things-in-an-array-even-hashes.md) - [Zero Padding](ruby/zero-padding.md) +### Shell + +- [Check If The First Argument Is Given](shell/check-if-the-first-argument-is-given.md) + ### tmux - [Adjusting Window Pane Size](tmux/adjusting-window-pane-size.md) diff --git a/shell/check-if-the-first-argument-is-given.md b/shell/check-if-the-first-argument-is-given.md new file mode 100644 index 0000000..fc668a5 --- /dev/null +++ b/shell/check-if-the-first-argument-is-given.md @@ -0,0 +1,19 @@ +# Check If The First Argument Is Given + +In a shell script, you may want to check if an argument was given. Each +argument is referenced numerically with the `$` prefix, so the first argument +is `$1`. To check if the first argument is given, you can use the `-z` check. + +```bash +if [ -z "$1" ] + then + echo "The first argument is missing" + exit 1 +fi +``` + +The `-z` checks if the argument is a zero-length string (so `""` or undefined +will be true). If it is missing, then we echo out a message and exit the +script. This is how I might fashion a script that requires the first argument. + +[source](https://stackoverflow.com/questions/6482377/check-existence-of-input-argument-in-a-bash-shell-script)