How to Use $0 and $1 in a Shell Script
You may come across, as well as use, a couple of positional parameters when dealing with shell scripts. This is because positional parameters are built-in variables, and they are vital for using command-line arguments in a shell script.
Here you will find out:
- what positional parameters are
- what the $0 and $1 in a shell script mean
- whether it is important to know positional parameters or not
- when DiskInternals can help you
Are you ready? Let's read!
Positional parameters
For example, you can have $N, where N is the positional variable, and it must be a single digit.
However, you must enclose a positional parameter with brackets when it is expanded and has more than one digit: (${N}).
To set and/or unset positional parameters, you need to use the shell’s built-in set and shift commands.
Note: positional parameters can be 1, 2, 3, 4, 5, etc. This implies you’ll write them this way: $1, $2, $3, $4, $5, etc. Furthermore, 0 is a positional parameter, but it is set as the script’s name.
What do the $0 and $1 in a shell script mean?
Having learned that positional parameters are donated by $0, $1, ….$N, you might ask: what do $0 and $1 entail? $0 will expand to output the name of your shell script or the shell itself. On the other hand, $1, $2,… $N are the arguments for the $0 command.
Example:
Practical example:
Using $0 and $1 in a shell script
These practical examples show how to use $0 and $1 in shell scripts:
When you run a script named test.sh:
The output will be:
Note: if you run the above script using bash, $0 will return test.sh instead of ./test.sh.
Thus, in simple terms:
- $0 outputs the "program_or_script_name"
- $1 outputs "the_first_word"
- $2 outputs "the_second_word"
Why is it important to know positional parameters?
The main reason why you should know and understand these positional parameters is because they will help you to write and read shell scripts properly. Interestingly, these parameters can be used in any bash/shell script. They can make a script appear very complex to someone who doesn’t understand positional parameters, but for those who understand positional parameters, they make things easier.
It’s so easy to open Linux files from Windows
You may not believe it, but it’s true: you can easily access and open your Linux files on a Windows PC. DiskInternals Linux Reader is a handy freeware program specially designed for dual-boot and virtual machines users. It is an intuitive software tool that works with most file systems: Ext2/3/4, ReiserFS, Reiser4, HFS, HFS+, FAT, exFAT, NTFS, ReFS, UFS2, RomFS(reader), ZFS (preview only*), XFS (preview only*), Hikvision NAS and DVR (preview only*).




Also, DiskInternals Linux Reader displays all your Linux partitions in a straightforward interface and lets you access them at ease to find your Linux files.
Related articles
- About a bash date command
- How to Access Linux Ext2 or Ext3 on Windows
- How to Access Ext4 from Windows
- How to Mount Ext4 on Windows for Free
- Mount Linux Drive on Windows for Free
- Bash Script: All You Need to Know
- An Algorithm: How to Create Bash While Loop
- Linux Shell: What You Need to Know at First
- 5 Basic Shell Script Examples for Your First Script
- Bash: How to Check if the File Does Not Exist
- Bash Time Command on Linux
- How to use bash get script directory in Linux
- How to Check Bash String Equality
- How to Use Linux Wait Command
- Bash: How to Loop Through Files in Directory
- 20 Examples of Bash Find Command
- Bash Cat Command in Examples
- Bash Script SSH: How to Use It
- Bash: A Script For User Input
- Linux ZSH: the basic you need to know
- Basic of Grep in Linux Shell Script
- Shell Script Cut: Basic You Need to Know
- Bash: How to Check if String Not Empty in Linux
- Bash: how to split strings in Linux
- Linux: Sudo in Bash Scripts
- Hello World Script in Bash
- A Linux bin/bash Shell
- Linux: New Line in Shell Script
- Linux: Bash String Ends With
- Linux: Bash Printf Examples
- Linux: Bash First Line
- Linux: A Bash Startup Script
- Linux: Write a Shell Script
- Shell Script: Replace String in File
- A crontab service shell script
- Bash and sh: is there any difference
- A Bash Dirname Command
- Linux: A Bash Source Command
- A Bash to Loop through Lines in File
- Linux: A Bash Linter
- A Bash Nested Loop
- A Bash Test Command
- Use a Shell Script to Send an Email
- A Bash Status of Last Command
- Linux: A Bash Basename Command
- Using Bash to Write to a File
- About Bash Language
- Bash for Loop in One Line
- AWK in a Bash Script
- Learn about useful bash scripts
- Learn about systemd startup script
- About chaining bash commands
- Learn about a bash error code
- Using a bash tee command
- About a bash export command
- Learn about a bash wait command
- Arch Linux install script
- About advanced bash scripting
- To run a shell script in Dockerfile
- About a bash UNTIL loop
- Learn about C shell script
- Learn about Korn shell scripting
- Using /usr/bin/env command
- Whether bash waits for command to finish
- Using bash if 0
- Using && in an IF statement in bash
- If you want to run shell script in background
- The disk you inserted was not readable by this computer error
- Learn about SFTP in bash scripting
- Learn to run Perl script in Linux
- Examples of using the Expect
- Copy command in shell scripts
- Shell script usage
- Install Oracle Database
- Here is everything you need to know about Bash for Windows
- Guide in Pictures
- How to Use Shell Script Sleep Command
- A Bash Multiline Command
