Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
df Command in Linux with Examples
Next article icon

declare command in Linux with Examples

Last Updated : 25 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The built-in is a powerful built-in feature of the Bash shell. It allows users to declare and set attributes for variables and functions, enabling better control over their behavior. By understanding how to use declare, you can manage variables and functions more effectively in your shell scripts.

This guide provides a detailed explanation of the declare command with examples, usage scenarios, and common options to help you get the most out of it.

Syntax

declare [options] [name[=value]] [name[=value]] ...

Basic declare Command Example

In this example, we will declare a variable using the declare command.

Bash

The output of the above typed commands is as follow:

Key Options for declare Command

1. -p:

This is used to displays the options and attributes of each variable name if it is used with name arguments.

Example:

2. -f:

Each name is treated as the name of function, not variable. So you can say this option restrict action or display to function names and definitions.

declare -f

3. -F:

When showing information regarding a function, it show only the function’s name and attributes. So the contents of the function will not be displayed.

declare -F

4. -g:

It causes all operations on variables to take effect in global scope.

Example:

function myfunc {
declare -g myglobal="I am global"
}
myfunc
echo $myglobal # Prints "I am global"

Note: For more details, you can use –help option with “declare” command as follows:

Conclusion

The declare command in Bash is an incredibly versatile tool for managing variables and functions. By using its various options, you can control how variables behave within your scripts—whether as integers, arrays, read-only values, or exported to subshell.

Additionally, declare helps you manage function definitions and display variable attributes, making it an essential part of advanced shell scripting.

What does the declare command do in Bash?

The declare command is used to declare variables and functions, set their attributes (such as readonly or integer), and display their values or definitions.

How do I declare an array in Bash?

You can declare an array using the -a option:

declare -a myarray

Can I make a variable read-only with declare?

Yes, the -r option allows you to declare a variable as read-only, preventing its value from being modified.

How do I display all function definitions using declare?

Use the -f option to display all function definitions:

declare -f

Next Article
df Command in Linux with Examples
author
shivangsharma15
Improve
Article Tags :
  • Linux-Unix
  • linux-command
  • Linux-Shell-Commands

Similar Reads

  • cpp command in Linux with Examples
    cpp is the C language preprocessor, it is automatically used by your C compiler to transform your program before compilation. It is also termed as a macro processor because it is used to give abbreviations for the longer piece of code. It can only be used with C, C++ and Objective-C source code. Usi
    3 min read
  • cron command in Linux with Examples
    The cron is a software utility, offered by a Linux-like operating system that automates the scheduled task at a predetermined time. It is a daemon process, which runs as a background process and performs the specified operations at the predefined time when a certain event or condition is triggered w
    4 min read
  • 'crontab' in Linux with Examples
    If you do manually backups , update logs, or restart services on your Linux machine? Imagine that running repetitive tasks overnight so your machine works for you while you rest. Here crontab, the native job scheduler in Linux, which enables users to easily automate commands, scripts, and system tas
    9 min read
  • csplit command in Linux with examples
    The 'csplit' command is used to split any file into many parts as required by the user. The parts are determined by context lines. Output pieces of FILE separated by PATTERN(s) to files 'xx00', 'xx01', ..., and output byte counts of each piece to standard output. Here, we will explain how to use 'cs
    3 min read
  • ctags command in Linux with examples
    The ctags command in Linux is commonly used with classic editors such as vi or vim to create an index or tags file for source code. This allows quick navigation across files, enabling users to jump to function definitions or references. ctags generates a cross-reference file that lists various sourc
    4 min read
  • cupsd command in Linux with examples
    cupsd is a type of scheduler for CUPS (Common Unit Printing System). It implements the printing system on the basis of the Internet Printing Protocol(Version 2.1). If no options is being specified on the command-line then the default configuration file /etc/cups/cupsd.conf will be automatically be u
    3 min read
  • curl Command in Linux with Examples
    curl is a command-line utility for transferring data to or from a server, employing a range of internet protocols such as HTTP, HTTPS, FTP, SCP, and SFTP. Whether you want to download a file, test a REST API, or simply verify that a website is up and running, curl is your best friend. It is accessed
    5 min read
  • cut command in Linux with examples
    The cut command in linux is a command for cutting out the sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character, and field. The cut command slices a line and extracts the text. It is necessary to specify an optio
    9 min read
  • cvs command in Linux with Examples
    In today's digital era, where file modifications and version control are essential, the Concurrent Versions System (CVS) command in Linux emerges as a powerful tool. CVS allows users to store and track the history of files, enabling easy retrieval of previous versions and restoring corrupted files.
    6 min read
  • How to Display and Set Date and Time in Linux | date Command
    Unlock the full potential of the date command in Linux—a versatile tool that does more than just show the current date and time. With this command, you can set your system’s clock, synchronize time across networks, and even calculate past or future dates for tasks like scheduling or logging. In this
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences