mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Check If The First Argument Is Given as a shell til
This commit is contained in:
19
shell/check-if-the-first-argument-is-given.md
Normal file
19
shell/check-if-the-first-argument-is-given.md
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user