Best Way to Declare\ BASH Var
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2016
    Location
    Cape Canaveral
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best Way to Declare\ BASH Var

    Hello,

    In a BASH shell script or .bashrc which way is viewed as the best practice in declaring an environment variable that needs to be exported?

    Example 1:
    Code:
    export MYVAR1="abc";

    Example 2:
    Code:
    MYVAR2="xyz"
    export MYVAR2

    Edit:
    I'd like to do it on one line using Example 1, that is without a new line character, but not sure if that is not as ideal as using two lines.



    Thanks,

    Michael
    Last edited by micahel8773; 15th May 2019 at 08:24 PM.

  2. #2
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    3,092
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Re: Best Way to Declare\ BASH Var

    I generally use
    export name=value

    The exception would be where the script had to perform various operations or take various paths in determining a value for the variable.
    I would then put the "export name" after a value was assigned.

    User error. Please replace user and try again

  3. #3
    Join Date
    Nov 2006
    Location
    Detroit
    Posts
    6,784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Best Way to Declare\ BASH Var

    Quote Originally Posted by micahel8773
    Example 1:
    Code:
    export MYVAR1="abc";
    Example 2:
    Code:
    MYVAR2="xyz"
    export MYVAR2
    I don't think it matters much either way. For compactness, you could declare your variables in several places and then export them all at the same time:
    Code:
    var1="value1"
    ...
    var2="value2"
    ...
    var3="value3"
    ...
    export var1 var2 var3
    I tend to do that in most of my scripts.
    OS: Fedora 29 x86_64 | Machine: Lenovo ThinkCentre M91P | CPU: Intel Core i5-2500 3.30GHz | RAM: 8GB PC3-12800 DDR3 | Disk: 500GB SATA | Video: Intel HD Graphics 2000 128MB | Sound: Realtek ALC662 | Ethernet: Intel 82579LM

  4. #4
    Join Date
    Feb 2005
    Location
    London, UK
    Posts
    925
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Re: Best Way to Declare\ BASH Var

    I write a lot of shell scripts and I very rarely need to export a variable, simply VAR="value" is usually sufficient. What sub processes are you running that need the variable to be exported?

  5. #5
    Join Date
    Nov 2016
    Location
    Cape Canaveral
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Best Way to Declare\ BASH Var

    Thanks - great suggestions!

    Quote Originally Posted by HaydnH
    What sub processes are you running that need the variable to be exported?
    Usually Oracle related: ORACLE_BASE, ORACLE_HOME etc.

Similar Threads

  1. [SOLVED]
    $BASH vs $SHELL; /usr/bin/bash vs /bin/bash
    By teancum144 in forum Using Fedora
    Replies: 4
    Last Post: 9th January 2015, 05:46 AM
  2. [BASH] PS1 variable and bash prompt
    By Adunaic in forum Programming & Packaging
    Replies: 0
    Last Post: 21st February 2013, 06:14 PM
  3. [SOLVED]
    BASH :: Scrollbar :: How To Create A CL Scrollbar Using Bash
    By cinsevarg in forum Programming & Packaging
    Replies: 5
    Last Post: 3rd December 2010, 09:19 PM
  4. Where should I declare the global variable
    By ahm_irf in forum Programming & Packaging
    Replies: 12
    Last Post: 24th May 2007, 07:11 PM
  5. Get bash completion working on fedora 3 (bash 3.0)
    By madcat in forum Guides & Solutions (Not For Questions)
    Replies: 2
    Last Post: 27th November 2004, 12:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •