declare command in Linux with Examples
Last Updated :
25 Sep, 2024
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.

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
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 'csp
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
3 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
8 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