InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Bash shell scripting

  • Bash - add a number to a variable
  • Bash - append text to a variable
  • Bash - how to check if a variable is set
  • Bash - how to compare file timestamps
  • Bash - how to find last command exit status code
  • Bash - how to get main program and current file dir location
  • Bash - how to redirect stderr to stdout or file
  • Bash - how to run custom commands at script exit
  • Bash - how to stop at error
  • Bash - how to use functions - quick tutorial
  • Bash - iterate over array
  • Bash - local and global variables
  • Bash - newline and other escape character in string
  • Bash - pass all arguments from one script to another
  • Bash - set default value if a variable is empty
  • Bash - variables in double quotes vs without quotes
  • Bash associative array tutorial
  • Bash check if file begins with a string
  • Bash shell - check if file or directory exists
  • Can global variables be modified in bash function?
  • Find memcache request hit rate on linux command line
  • How to return a value from bash function
  • Iterate over specific file extension in a dir in shell script
  • Linux - Yesterday's Date in YYYYMMDD format
  • bash - extract urls from xml sitemap
  • bash - how to use regex in if condition
 
  • Home
  • > Tutorials
  • > Bash shell scripting

Bash – local and global variables

By admin on Jan 5, 2016

Bash functions can have local variables. This can protect the accidental modification of global variables when function is called inline within same shell. Here are some examples.

Bash function with global variable

The global variable is modified inside function when called within same shell.

#!/bin/bash
function f1 () {
  echo "in f1 x=$x"
  x="HelloNew"
}
x="Hello"
f1
echo "outside f1 x=$x"
in f1 x=Hello
outside f1 x=HelloNew
Env: GNU bash, version 4.2.46

The global variable is not modified inside function when called in sub-shell.

#!/bin/bash
function f1 () {
  echo "in f1 x=$x"
  x="HelloNew"
}
x="Hello"
somevar=$(f1)
echo "outside f1 x=$x"
outside f1 x=Hello
Env: GNU bash, version 4.2.46

Note that in both cases (same shell and sub-shell), global variables can be read and used inside function. Only when executed in same shell, modification is visible outside function.

Bash function with local variable

Example of bash function with local variable.

#!/bin/bash
function f1 () {
  local x="HelloNew"
}
x="Hello"
f1
echo "outside f1 x=$x"
outside f1 x=Hello
Env: GNU bash, version 4.2.46

Bash function with multiple local variable

Example of bash function with multiple local variable.

#!/bin/bash
function f1 () {
  local x="HelloNew",y="HelloNew"
}
x="Hello"
y="Hello2"
f1
echo "outside f1 x=$x"
echo "outside f1 y=$y"
outside f1 x=Hello
outside f1 y=Hello2
Env: GNU bash, version 4.2.46

Bash – protect a variable by making it readonly

In case you have global variables which are initialised only once, you can make them readonly.

#!/bin/bash
function f1 () {
  x="HelloNew"
}
declare -r x="Hello"
f1
echo "outside f1 x=$x"
outside f1 x=Hello
test.sh: line 3: x: readonly variable
Env: GNU bash, version 4.2.46

Suggested posts:

  1. Install Amazon EC2 api tools on Ubuntu Linux
  2. CSS – align div in center horizontally
  3. How to specify environment variable for a command on Linux
  4. Bash – newline and other escape character in string
  5. Can global variables be modified in bash function?
  6. How to show environment variable for a process id (pid)
  7. Can ssh keys generated as one user be used as a different user
  8. Bash – append text to a variable
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Bash shell scripting, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress