• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

How to pass arguments in Bash Script on Ubuntu 22.04

  • 00:51 lsb_release -a
  • 01:20 vim argument.sh
  • 02:37 ls -la
  • 02:56 chmod +x argument.sh
  • 02:58 ls -la
  • 03:14 ./argument.sh Hi linuxhelp
  • 03:41 vim argument.sh
  • 05:19 ./argument.sh hello im linuxhelp
  • 06:06 vim argument1.sh
  • 07:55 chmod +x argument1.sh
  • 08:05 ./argument1.sh
  • 08:17 ./argument1.sh Linuxhelp
{{postValue.id}}

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.

Tags:
mason
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What are $1 and $2 in the Bash script?

A

$0 - The name of the script. $1 - The first argument sent to the script. $2 - The second argument sent to the script.

Q

How do you handle arguments in a Bash Script?

A

We can use the getopts program/ command to parse the arguments passed to the script in the command line/ terminal by using loops and switch-case statements.

Q

How do you check if there is an argument in Bash?

A

The -n test operator checks whether the given string operand has a non-zero length. If it does, then the expression returns true. The -z test operator checks if the given string operand has a zero length.

Q

How do I give multiple arguments in Bash?

A

You can pass more than one argument to your bash script. In general, here is the syntax of passing multiple arguments to any bash script: script.sh arg1 arg2 arg3

Q

What is the symbol for all arguments in Bash?

A

The special character $# stores the total number of arguments. We also have $@ and $* as wildcard characters which are used to denote all the arguments.

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help keel johnston ?
Unhide the folders on windows Explorer

Give any solutions to unhide folder using command prompt?

forum3

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.