Frage

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?

War es hilfreich?

Lösung 2

They are available, but they are not exported so scripts launching from command-line don’t get these variables. If your .zshrc looks like

function zshrc()
{
    local VAR=1
    # Do stuff
}
zshrc

and you then never want to launch zshrc function again you can just do

unfunction zshrc

afterwards.

Andere Tipps

If you're using a recent version of zsh you can use an anonymous function:

function () {
  local xyz=abc
  # whatever
}

The function will be automatically executed and then thrown away, it exists only for scoping purposes.

This works for any sourced file, not only zshrc.

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

  1. Open a new terminal window.
  2. 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 variable is scoped to the function name method_name and only available inside of the function when called (and 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, environment variables are 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 environment variables and their respective shells

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

You can quickly view environment variables with env or printenv

The convention is to use UPPERCASE

To temporarily set an environment 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 or .bashrc or whichever you are using you can now use this environment 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 environment 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 that environment variable, remove it from the file and again source the file.

To temporarily remove an environmental variable, use unset

Example

unset B

Note there is no $ when unsetting.

To set environment variables from the command line

export BLABLA="environment 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.

There is apparently a bash convention to name 'private' functions with double underscore .. of course they are not actually private . I am using this convention in my .zshrc.

function __comment()
{
  curr=`pwd`
  echo "$curr $*"
}

__comment 'Here is a Comment'
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top