• 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 use Flags on Bash Script on Ubuntu 22.04

  • 00:36 lsb_release -a
  • 00:53 vim test.sh
  • 04:15 ls -la
  • 04:26 chmod +x test.sh
  • 04:31 ls -la
  • 04:41 ./test.sh
  • 04:56 ./test.sh -x
  • 05:05 ./test.sh -y
  • 05:17 ./test.sh -z
{{postValue.id}}

To Use Flags On Bash Script On Ubuntu 22.04

Introduction:

Handling flags in Bash scripts allows users to customize the script's behaviour and provides greater flexibility and usability. The getopts command is suitable for handling short options, while an extended approach enables handling both short and long options.

Procedure:

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: Create a file using your favourite editor, here I’m using vim editor

root@linuxhelp:~# vim test.sh
Copy the following script in the file
#!/bin/bash
mem(){
        free -h | grep Mem
}
disk(){
        df -h /
}
load(){
        uptime
}
while getopts 'xyz' OPTION; do
        case "$OPTION" in
                x)
                        mem
                        ;;
                y)
                        disk
                        ;;
                z)
                        load
                        ;;
        esac
done

Step 3: Now long list the directory by using the below command

root@linuxhelp:~# ls -la
total 56
drwx------  4 root root 4096 Nov 15 01:39 .
drwxr-xr-x 20 root root 4096 Sep 11 22:13 ..
-rw-------  1 root root  426 Nov 14 00:48 .bash_history
-rw-r--r--  1 root root 3106 Oct 15  2021 .bashrc
drwx------  2 root root 4096 Sep 11 17:28 .cache
-rwxr-xr-x  1 root root  197 Nov 15 01:20 flag.sh
-rw-------  1 root root   20 Nov 15 01:21 .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  201 Nov 15 01:39 test.sh
-rw-------  1 root root 8936 Nov 15 01:39 .viminfo
-rw-------  1 root root   55 Sep 11 17:28 .Xauthority

Step 4: There is no execute permission for the file, so give execute permission by using the following command

root@linuxhelp:~# chmod +x test.sh

Step 5: Check the execute permission by listing the directory by using the below command

root@linuxhelp:~# ls -la
total 56
drwx------  4 root root 4096 Nov 15 01:39 .
drwxr-xr-x 20 root root 4096 Sep 11 22:13 ..
-rw-------  1 root root  426 Nov 14 00:48 .bash_history
-rw-r--r--  1 root root 3106 Oct 15  2021 .bashrc
drwx------  2 root root 4096 Sep 11 17:28 .cache
-rwxr-xr-x  1 root root  197 Nov 15 01:20 flag.sh
-rw-------  1 root root   20 Nov 15 01:21 .lesshst
-rw-r--r--  1 root root  161 Jul  9  2019 .profile
drwx------  5 root root 4096 Sep 11 22:24 snap
-rwxr-xr-x  1 root root  201 Nov 15 01:39 test.sh
-rw-------  1 root root 8936 Nov 15 01:39 .viminfo
-rw-------  1 root root   55 Sep 11 17:28 .Xauthority

Step 6: Now run the script without flags by using the below command

root@linuxhelp:~# ./test.sh

Step 7: Now run the script with -x flag (Which shows the memory usage) by using the below command

root@linuxhelp:~# ./test.sh -x
Mem:           3.8Gi       1.0Gi       1.3Gi        45Mi       1.4Gi       2.5Gi

Step 8: Now run the script with -y flag (Which shows the storage info of the / directory) by using the below command

root@linuxhelp:~# ./test.sh -y
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        39G   13G   24G  36% /

Step 9: Now run the script with -z flag (Which shows the system uptime, No of user and load average) by using the below command

root@linuxhelp:~# ./test.sh -z
 01:41:02 up 35 min,  1 user,  load average: 0.04, 0.07, 0.16

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to use Flags on Bash Script. Your feedback is much welcome.

Tags:
noah
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

How to use the flag on the Bash script?

A

Flags usually begin with a single hyphen (-) followed by a single letter or a double hyphen (–) followed by a full word

Q

How to use the flag on the Bash script?

A

Flags usually begin with a single hyphen (-) followed by a single letter or a double hyphen (–) followed by a full word

Q

What does the E flag mean in bash?

A

The -e flag in both the Bourne Shell and C shell causes the shell to exit if any command fails.

Q

How do you handle flags in bash?

A

The getopts command is suitable for handling short options, while an extended approach enables handling both short and long options.

Q

Which flag shows verbose output for a bash script?

A

We can enable the verbose mode using the -v switch, which allows us to view each command before it's executed.

Load more

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 David Lopez Guillen ?
Ayuda urgente instale SSL para servidor Opensuse y ahora no funciona tengo servicio web

hola segui este tutorial para tener un certificado ssl y ahora no se ve mi app en la red, espero alguien pueda ayudarme, tengo M9oodle en3.5 en un servidor open suse y ahora no funciona por favor ayuda.

https://www.linuxhelp.com/how-to-create-ssl-certificate-in-opensuse

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.