0% found this document useful (0 votes)
5 views14 pages

Linux

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views14 pages

Linux

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Name: Chandan Kushwaha

Course: B.Tech(cse),2nd year:


Section: 2
QID: 23030734
Subject: Linux and Open
Source
Assignment: 2
TABLE OF CONTENT

1. Variables &
Introduction
2. Exporting &
Declaration of
Variables
3. Functions &
Introduction
4. Declaration of
Functions
5. Key Points and
Exporting Variables and
Functions in Linux
This presentation will explore the concepts of exporting variables and
functions in Linux, a fundamental aspect of scripting and system
administration.
Introduction to Variables in Linux
Variables are placeholders used to Variables in Linux are case-sensitive, You can declare a variable by using
store data in Linux scripts. They are meaning "my_variable" is different the syntax "variable_name = value"
crucial for storing temporary values from "MY_VARIABLE". They are also in a script, assigning a specific value
and information that is reused dynamically typed, meaning the to a variable.
throughout your scripts. data type of a variable is determined
at runtime.
Declaring and Assigning
Variables
1 1. Declaration 2 2. Assignment
Begin by declaring the Next, assign a value to the
variable name. For variable using the
example, you can use assignment operator (=).
"MY_VAR" for a variable. For instance, "MY_VAR =
Use a descriptive name "hello" would assign the
that reflects the variable's string "hello" to the
purpose. variable.

3 3. Usage
You can access the variable's value using its name. For
example, "echo $MY_VAR" would print the value "hello" to the
console.
Exporting Variables in Linux
Scope
Variables in Linux have a scope. Local variables exist only within the current script, while global
1
variables are available across scripts.

Exporting
2 The 'export' command is used to create a global variable that can be accessed by
other scripts or processes. It's crucial for sharing data between multiple scripts or
environments.

Syntax
3 To export a variable, use the syntax "export variable_name=value". This
makes the variable available across different environments within the same
shell session.

Example
4 The command 'export MY_VAR="hello"' would export the variable
"MY_VAR" with the value "hello" to be accessible globally.
Accessing Exported Variables

Direct Access
You can access an exported variable directly using its name, prepended with the dollar
sign ($). For example, "$MY_VAR" will retrieve the value of the exported variable
"MY_VAR".

Environment Variables
The 'env' command lists all environment variables, including exported variables. You can
search for a specific variable within the list to confirm its value.

Shell Session
Exported variables are persistent within the current shell session. Closing the session will
lose the exported variable, but you can re-export it again if needed.
Introduction to Functions in Linux
Modularity
1 Functions are reusable blocks of code that perform specific tasks. They promote code organization and
readability, breaking down complex tasks into smaller, manageable chunks.

Efficiency
Functions can be called multiple times within your script, reducing code repetition
2
and making your script more efficient. They prevent redundancy and streamline
code execution.

Parameters
Functions can take parameters, allowing them to receive
3
data and process it accordingly. This enhances flexibility and
makes your code more adaptable to various scenarios.
Declaring Functions
Syntax Name
The basic syntax for declaring a Choose a descriptive name for
function in a Linux script is: your function that reflects its
function_name() purpose. Follow the same
{ commands; }. naming conventions as
variables, using lowercase
letters and underscores.

Body Return Value


The body of the function Functions can optionally return
contains the set of commands values. Use the 'return'
that it will execute. These statement to specify the value
commands should be indented that will be returned when the
for better readability. function completes its
execution.
Executing Functions

1 2 3
Calling Arguments Output
To execute a function, simply call it by If the function expects parameters, The function's output will be
its name followed by any required you must pass them in the call. The displayed based on the commands in
parameters. arguments are separated by spaces. its body. You can use the 'echo'
command to display messages or
values.
Exporting Functions in Linux

To export a function in Linux, you can use the 'export' command. This makes the function available globally and
accessible to other scripts.
Conclusion and Key
Takeaways
We've covered the basics of exporting variables and functions in
Linux, which are essential for managing your scripts and systems
effectively.
THANK
YOU !

You might also like