Dowemo
0 0 0 0


Question:

Any variable that I declare in my zshrc is available in the shell as an environment variable. I don't want this to happen.

I tried putting the variables in a function and setting them as local, but then the function is available outside of the zshrc.

How can I make it so what happens in my zshrc stays in my zshrc?


Best Answer:


If you do not append the word local to a variable it will remain until you do one of the following:

  • Open a new terminal window.
  • Run exec zsh or exec bash depending on your shell. This just clears out your local variables that were not assigned with the word local.
  • Avoid this

    method_name(){
      a=11
      echo $a
    }

    Correct Example

    method_name(){
      local a=11
      echo $a
    }

    This a variable is scoped to the function name method_name and only avaiable inside of the function when called and also not afterwards.

    If you want direct access to that local variable you can set it this way

    local z=11

    And call it this way

    echo $z

    Additionally, environmental variables are a different from local variables

    Depending on your shell and needs you may use .bash_profile or .bashrc or .zshrc etc. to store functions and aliases.

    View this reference for more on environmental variables and their respective shells

    Also read this to understand how to set environmental variables on the command line using shell expansions

    You can quickly view environment variables with env or printenv

    The convention is to use UPCASE

    To temporarily set an environmental variable (stored until you close the terminal)

    export A=11 or export B="11 is part of this string"

    Assuming you have opened a new terminal window or sourced .zshrc .bashrc or whatever one you are using you can now use this environmental variable until you close your terminal session. Note: do not use $ when setting, but do use $ when referencing the variable.

    Examples

    echo "A is equal to: $A and that is pretty nice"

    echo "$A"

    How to source a file

    source ~/path/to/file/filename

    Example

    source ~/.bash_profile

    To set an environmental variable (until you remove it or set it again)

    Use the code above but place it in your ~/.bash_profile or ~/.zshrc or other respective file. Save the file and source it.

    Example

    export B="11 is part of this string"

    You now can view it with

    env

    To remove a that environmental variable, remove it from the file and again source the file.

    To temporarily remove that environmental variable, use unset

    Example

    unset B

    Note there is no $ when unsetting.

    To set enviromental variables from the command line

    export BLABLA="environmental variable set from the command line, saved in file for later use"

    Check the file you are sending it to, it may not start on a new line, it might have been concatenated to your last line which was some other function, alias or other.

    This is not a fully comprehensive answer, but it is a great step in the right direction. It shows how scope in a terminal shell can be set, used and removed.




    Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs