Shell Script Argument Reading: Command Line Parameters
, if you write a shell script but do not use arguments… It means you are driving the Ferrari in 1st gear.
In this guide, you will learn the command line arguments that enable scripts to receive data from outside in a clear and clean way.
What Will You Learn in This Guide?
- What command line arguments do
- Special Bash variables like
$0,$1,$#,$@ - How to write a simple but powerful shell script that works with arguments
- Interpret the outputs correctly
What are Command Line Arguments?
Arguments are the parameters you add to a script when you run it.
In Linux these are called positional parameters.
Example:
./arguman_test.sh merhaba dunya
- This command sends two different arguments to the script.
Bash Special Variables (Golden List)
-
Shell automatically assigns arguments to these variables:
-
$0 → Name of the script
-
$1 → First argument
-
$2 → Second argument
-
$9 → Ninth argument
-
$# → Total number of arguments
-
$@ → All arguments (in list form)
-
$$ → Process ID of the script
$9? → Exit code of last command
Implementation: Argument Reading Shell Script Now let's get into the code.
1. Create Script File
nano arguman_test.sh
- This command opens a new shell script file.
2. Script Content
#!/bin/bash
echo "Betik Adı: $0"
echo "Birinci Parametre: $1"
echo "İkinci Parametre: $2"
echo "Tüm Argümanlar: $@"
echo "Toplam Argüman Sayısı: $#"
- This script prints all submitted arguments to the screen.
3. Grant Run Permission and Start
chmod +x arguman_test.sh
./arguman_test.sh merhaba dunya
- The script is run with two arguments.
Expected Logic
-
$1 → hello
-
$2 → world
-
$# → 2
-
$@ → hello world
- Everything is clear, everything is under control 😎
Frequently Asked Questions (FAQ)
1. Can I use more than 9 arguments? Yes. You should use it as ${10}, ${11}.
2. What happens if the argument is empty? The variable remains empty. You can check it with if [ -z "$1" ]
3. If there is a space inside the argument? You should use quotes:
./betik.sh "uzun bir cumle"
__4. $@ or $?* "$@" is safer. Use it in prod environment.
Result
Command line arguments turn shell scripts from static files into real automation tools. You can manage hundreds of scenarios with a single script.
If you want to run this type of automation on high-performance Linux servers, you can test it immediately on the GenixNode infrastructure 🚀

