Env and BASH declare

Env and BASH declare

Created:11 Jan 2017 12:58:48 , in  Host development

Environmental variables on a Linux based system like Debian are named strings available to all applications initialized from text console or some graphical interface.

These variables are gradually added by various processes as your system starts. Once login shell is invoked and reads its configuration files, environmental variables are ready to use.

Environmental variables you see often are PATH, UID, MAIL, HOME, USER and more. They are in caps.

env

To find out what environmental variables are available to you, you can use the env command in your terminal.

The command displays the variables in the form of NAME=VALUE where VALUE is a string.

Env command is not just for displaying environmental variables, it also enables you to change what environmental variables will be passed to a program run with it.

Type:


$ whatis env
to see that.

When you need to run a program using env, you do it as follows:

$ env bash --posix

The above will give you a new BASH shell working in POSIX mode.

It is a common practice to use env in the first line of your BASH script like this:


#!/usr/bin/env bash
.
.

#!/usr/bin/env -i bash --posix
.
.

The above line of code will invoke BASH in POSIX mode with all environmental variables ignored.

Env is a standalone program, you can find out more about how it works reading its manual page.


$ man env

Declare

If you need another, a more detailed way to browse variables (not just environmental ones but also other already available). You can use BASH builtin declare.

To examine a variable using declare you type in your text console:


$ declare -p VARIABLE

The code like this displays not only value of the VARIABLE but also provides some information on what the VARIABLE is in terms of its type. It could be a string, an integer, an indexed array or perhaps an associative array. Declare, also displays whether the variable will be exported (-x) to a child process or not and gives you an opportunity to turn off this and other VARIABLE's attributes.

If you need not VARIABLE available to your program, type:


declare +x VARIABLE 

BASH declare has a few useful options, type:


$ help declare  

to get detailed description.

A bit of testing follows:


$ declare -p

will print all the variables available to the current shell.

To see what variables exported to a child process:


$ declare -p | grep -- -x

Similar thing can be done for BASH running in POSIX mode. However outcome could be different due to different startup files being read by the shell ($ENV for POSIX and $BASH_ENV for standard mode) .

To check if BASH is in POSIX mode you can check if POSIXLY_CORRECT BASH variable is set:


[ -z "$POSIXLY_CORRECT" ] && { echo "not in POSIX mode"; } || { echo "in POSIX mode"; }

Since scripts are run in non-interactive shells, as opposed to interactive non-login shells people use for typing commands, it is good to know how to differentiate between these modes.

You test for "i" (stands for interactive) here:


$ grep -q "i" <<< "$-" &&  echo "interactive" || echo "non-interactive"

If it is just for your eyes, the test could also look like this:


$ declare -p $-

Declare in function body

Declare creates local variables when put in body of a function.


declare result=$((3**3))

getResult(){
  declare result=$((3**2)) # try to overvrite result
  return
}

getResult
echo $result # prints 27 

Use of declare with -n option makes it easy for function to return a value:


declare result=$((3**3))
getResult(){
  declare -n my_result=result && my_result=$((3**2)) # try to overwrite result
  return
}

getResult
echo $result # prints 9

Returning values this new way ( the option was added only in BASH version 4.3 ) is certainly faster than traditional echoing and subsequently recovering with subshell. On the other hand encapsulation of code might turn out to be more difficult to achieve with it.

This post was updated on 06 Oct 2021 20:55:43

Tags:  BASH 


Author, Copyright and citation

Author

Sylwester Wojnowski

Author of the this article - Sylwester Wojnowski - is a sWWW web developer. He has been writing computer code for the websites and web applications since 1998.

Copyrights

©Copyright, 2024 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.

Citation

Cite this article as:

Wojnowski, Sylwester. "Env and BASH declare." From sWWW - Code For The Web . https://swww.com.pl//main/index/env-and-bash-declare

Add Comment

Allowed BB Code - style tags: [b][/b], [i][/i], [code=text][/code],[code=javascript][/code],[code=php][/code],[code=bash][/code],[code=css][/code],[code=html][/code]


I constent to processing my data given through this form for purposes of a reply by the administrator of this website.

Recent Comments

Nobody has commented on this post yet. Be first!