Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8816823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:42:26+00:00 2026-06-14T04:42:26+00:00

I constantly fight with the decision of how to declare a variable or function

  • 0

I constantly fight with the decision of how to declare a variable or function in Bash.

Given the following assumptions:

  1. Bash is the only scripting language available.
  2. Naming conventions are irrelevant.

In the case of global variables should I use:

  1. foo=bar – inside and outside of functions?
  2. declare -g foo=bar – inside and outside of functions?
  3. local -g foo=bar – inside of functions?

In the case of local variables should I use:

  1. local foo=bar
  2. declare foo=bar

In the case of read-only variables should I use:

  1. declare -r foo=bar
  2. local -r foo=bar
  3. readonly foo – following [1.] or [2.] without the -r flag on the next line.

In the case of functions should I use:

  1. foo() { echo bar; }
  2. foo { echo bar; }
  3. function foo() { echo bar; }
  4. function foo { echo bar; }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:42:27+00:00Added an answer on June 14, 2026 at 4:42 am

    In order to forget about it I define the following near the top of my .bashrc as well as each of my Bash shell script files:

    # Allow to define an alias.
    #
    shopt -s expand_aliases
    
    # Defines a function given a name, empty parentheses and a block of commands enclosed in braces.
    #
    # @param name the name of the function.
    # @param parentheses the empty parentheses. (optional)
    # @param commands the block of commands enclosed in braces.
    # @return 0 on success, n != 0 on failure.
    #
    alias def=function
    
    # Defines a value, i.e. read-only variable, given options, a name and an assignment of the form =value.
    #
    # Viable options:
    #   * -i - defines an integer value.
    #   * -a - defines an array value with integers as keys.
    #   * -A - defines an array value with strings as keys.
    #
    # @param options the options. (optional)
    # @param name the name of the value.
    # @param assignment the equals sign followed by the value.
    # @return 0 on success, n != 0 on failure.
    #
    alias val="declare -r"
    
    # Defines a variable given options, a name and an assignment of the form =value.
    #
    # Viable options:
    #   * -i - defines an integer variable.
    #   * -a - defines an array variable with integers as keys.
    #   * -A - defines an array variable with strings as keys.
    #
    # @param options the options. (optional)
    # @param name the name of the variable.
    # @param assignment the equals sign followed by the value. (optional)
    # @return 0 on success, n != 0 on failure.
    #
    alias var=declare
    
    # Declares a function as final, i.e. read-only, given a name.
    #
    # @param name the name of the function.
    # @return 0 on success, n != 0 on failure.
    #
    alias final="readonly -f"
    

    The above definitions allow me to say for example:

    1. def foo { echo bar; }.
    2. final foo
    3. var foo=bar
    4. val foo=bar

    As indicated by the comments you can mix and match various variable flags such as var -g foo=bar for a global (-g) variable (var) or val -Ai foobar=([foo]=0 [bar]=1) for a read-only (val), associative array (-A) consisting of integer (-i) values.

    Implicit variable scoping too comes with this approach. Also the newly introduced keywords def, val, var and final should be familiar to any software engineer who programs in languages such as JavaScript, Java, Scala and the like.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm constantly confused by the rules for quoting and evaluating when I'm writing bash
I constantly need to attach new paths to the PATH environment variable in .bashrc,
I'm constantly receiving the following error in a very simple WebProject with 1 html
The variable 'totalBytes' is constantly at -1 so I cannot calculate/update the progress bar
I am constantly seeing the following postgres errors/warnings in my Rails development log. It's
I'm constantly doing the following pattern in Django: class MyModel(models.Model): FOO = 1 BAR
I'm constantly getting the following error on my app: 09-25 15:52:24.875: E/AudioHardware(144): -----AudioStreamInALSA::read(0x42db0040, 512)
I have the following test that is constantly failing and I cant work out
I constantly get malloc error : double freed.... The echoTime function is being called
I constantly see myself having to add the same extra variable to the context

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.