Last active
January 2, 2019 15:32
-
-
Save Hazem-Ben-Khalfallah/9d78bd6e7a0ca99fccd6a44ac188d413 to your computer and use it in GitHub Desktop.
[Bash args] Bash positional arguments and command line options usage #linux #bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #see link: http://linuxcommand.org/lc3_wss0120.php | |
| # Positional args | |
| # $#: number of items on the command line in addition to the name of the command | |
| # $0: name of the command | |
| > some_program word1 word2 word3 | |
| # $# would be 3 | |
| # $0 would contain "some_program" | |
| # $1 would contain "word1" | |
| # $2 would contain "word2" | |
| # $3 would contain "word3" | |
| # Detecting Command Line Arguments | |
| if [ "$1" != "" ]; then | |
| echo "Positional parameter 1 contains something" | |
| else | |
| echo "Positional parameter 1 is empty" | |
| fi | |
| if [ $# -gt 0 ]; then | |
| echo "Your command line contains $# arguments" | |
| else | |
| echo "Your command line contains no arguments" | |
| fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment