Shell command line parameters
From Linux Shell Scripting Tutorial - A Beginner's handbook
Most Linux command can take different actions depending on the command line arguments supplied to the command.
Contents |
What is a command line argument?
A command line argument is nothing but an argument sent to a program being called. A program can take any number of command line arguments. For example, type the following command:
ls grate_stories_ofSample Outputs:
grate_stories_of: No such file or directory.
ls is the name of an actual command and shell executed this command when you type command at shell prompt. The first word on the command line is:
- ls - name of the command to be executed.
- Everything else on command line is taken as arguments to this command.
Consider the following example:
tail +10 /path/to/file.txt
- tail : command name.
- +10 /path/to/file.txt : The arguments.
Examples
Try the following command and note down its command line arguments:
| Command | Command name | Total number of arguments | Argument name(s) |
|---|---|---|---|
| ls | ls | 0 | N/A |
| ls /etc/resolv.conf | ls | 1 | /etc/resolv.conf |
| cp /etc/resolv.conf /tmp/test.txt | cp | 2 | /etc/resolv.conf, and /tmp/test.txt |
| sort -r -n /path/to/file | sort | 3 | -r, -n, and /path/to/file |
| date +"%d-%m-%Y" | date | 1 | +"%d-%m-%Y" |
Command line parameters different names
A Command line parameter also known as:
- Command line options
- Options
- Positional parameters
- Flag
- Switches or switch
- Command-line arguments
Why use command line arguments
- Telling the command/utility which option to use.
- Informing the utility/command which file or group of files to process (reading/writing of files).