How to pass arguments in Bash Script on Ubuntu 22.04
To Pass Arguments In Bash Script On Ubuntu 22.04
Introduction
Arguments passed to a script are processed in the same order in which they’re sent. The indexing of the arguments starts at one, and the first argument can be accessed inside the script using $1. Similarly, the second argument can be accessed using $2, and so on. The positional parameter refers to this representation of the arguments using their position.
Procedure Steps:
Step 1: Check the OS version by using the below command
root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
Step 2: Now create a new file by using the vim editor
root@linuxhelp:~# vim argument.sh
Add the following lines in the file
#!/bin/bash
echo "1st parameter = $1"
echo "2nd parameter = $2"
Step 3: List the directory verify the file permission by using the below command
root@linuxhelp:~# ls -la
total 104
drwx------ 4 root root 4096 Nov 30 14:43 .
drwxr-xr-x 20 root root 4096 Sep 11 22:13 ..
-rw-r--r-- 1 root root 90 Nov 29 07:58 argument.sh
-rw------- 1 root root 8103 Nov 29 18:06 .bash_history
-rw-r--r-- 1 root root 3106 Oct 15 2021 .bashrc
drwx------ 2 root root 4096 Sep 11 17:28 .cache
-rw------- 1 root root 20 Nov 29 10:50 .lesshst
-rw-r--r-- 1 root root 161 Jul 9 2019 .profile
drwx------ 5 root root 4096 Sep 11 22:24 snap
-rw-r--r-- 1 root root 12288 Nov 28 11:09 .test.sh.swp
-rw------- 1 root root 10644 Nov 30 14:43 .viminfo
-rw------- 1 root root 165 Nov 30 14:41 .Xauthority
Step 4: There is no execute permission for the file, so give execute permission by using the below command
root@linuxhelp:~# chmod +x argument.sh
Step 5: Again list the directory and check the file permission by using the below command
root@linuxhelp:~# ls -la
total 104
drwx------ 4 root root 4096 Nov 30 14:43 .
drwxr-xr-x 20 root root 4096 Sep 11 22:13 ..
-rwxr-xr-x 1 root root 90 Nov 29 07:58 argument.sh
-rw------- 1 root root 8103 Nov 29 18:06 .bash_history
-rw-r--r-- 1 root root 3106 Oct 15 2021 .bashrc
drwx------ 2 root root 4096 Sep 11 17:28 .cache
-rw------- 1 root root 20 Nov 29 10:50 .lesshst
-rw-r--r-- 1 root root 161 Jul 9 2019 .profile
drwx------ 5 root root 4096 Sep 11 22:24 snap
-rw-r--r-- 1 root root 12288 Nov 28 11:09 .test.sh.swp
-rw------- 1 root root 10644 Nov 30 14:43 .viminfo
-rw------- 1 root root 165 Nov 30 14:41 .Xauthority
Step 6: Now run the script with arguments by using the below command
root@linuxhelp:~# ./argument.sh Hi linuxhelp
1st parameter = Hi
2nd parameter = linuxhelp
Step 7: Edit the file with vim editor by using the below command
root@linuxhelp:~# vim argument.sh
Add the lines in the file
#!/bin/bash
echo "The number of arguments passed are : $#"
echo "The arguments are : $@"
Step 8: Again run the script by using the below command
root@linuxhelp:~# ./argument.sh hello im linuxhelp
The number of arguments passed are : 3
The arguments are : hello im linuxhelp
Step 9: Now create another file with vim editor by using the below command
root@linuxhelp:~# vim argument1.sh
Add the lines in the file
#!/bin/bash
if [[ -z $1 ]]
then
echo "No parameter passed."
else
echo "Parameter passed = $1"
fi
Step 10: Give execute permission to the file by using the below command
root@linuxhelp:~# chmod +x argument1.sh
Step 11: First run the script with no arguments by using the below command
root@linuxhelp:~# ./argument1.sh
No parameter passed.
Step 12: Now run the script with arguments by using the below command
root@linuxhelp:~# ./argument1.sh Linuxhelp
Parameter passed = Linuxhelp
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to pass arguments in Bash Script on Ubuntu 22.04. Your feedback is much welcome.
Comments ( 0 )
No comments available