Bash - (Environment) Variable

Bash Liste Des Attaques Ovh

About

A variable is a parameters referenced by a name.

A variable (ie a name used to store data) in bash is called a parameter. A variable in bash is one of the three type of parameters.

A variable has:

  • a value
  • and zero or more attributes (such as integer, …). Attributes are assigned using the declare builtin command.

Syntax

Assignment

with assignment operator

In the shell command language, a variable is a word consisting of the following parts:

[local] name=[value]

where:

If the variable has its integer attribute set, then value is evaluated as an arithmetic expression even if the $((...)) expansion is not used.

All values undergo the following expansion:

Is not performed:

Assignment statements may also appear as arguments to the:

With multiple words

With the read command

read -r var1 var2 var3 <<< "val1 val2 val3"
echo $var1
echo $var2
echo $var3
val1
val2
val3

Usage

  • ${VAR1}
  • $VAR1 is a simplified version of ${VAR1} where the boundary is not defined.

${VAR1} is a parameter expansion notation.

Example:

var1=Hello
echo ${var1}
Hello

echo "$var1_Nico"
# Blank

echo "${var1}_Nico"
Hello_Nico

Type

Scope

See:

Example

varname=value
echo $varname
value

To create your own shell variables. First issue the command

newdir=$HOME/mynewdirectory

and then, regardless of what directory you are in, you can issue

cd $newdir

Shell Variable

The builtin, intern, system variable, reserved variable name, see: Bash - (Builtin|Intern|System|Reserved|Shell) variable name

Management

Display

One

To see an environment variable, you make use of the echo command as :

echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin

or with the env command

env | grep PATH

All with env

To list the current values of all environment variables, issue the command

env
HOSTNAME=ebs121.localdomain
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.2.2 3886 22
SSH_TTY=/dev/pts/1
USER=root
.........

or

$ env | more

All with declare

To list the current values of all environment variables, execute the declare command

declare

To list all names with their definitions, issue the declare command with p

declare -p
declare -- BASH="/bin/bash"
declare -r BASHOPTS="checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath"
declare -ir BASHPID
declare -A BASH_ALIASES='()'
declare -a BASH_ARGC='()'
..............

A subset

Thanks to the parameter expansion prefix, we can get a list of variable that begins with a prefix.

Example:

Prefix1=Prefix1Value
Prefix2=Prefix2Value
echo ${!Prefix*}
  • output:
Prefix1 Prefix2

Modify

For a session

If you set your variable in a script, you need to call it with the source command, otherwise they will not be available in the parent process

The syntax depends of your shell:

  • for the Bourne, Bash, or Korn shell:

To add /sbin to the path, type the export command in a console :

export PATH=$PATH:/sbin

or with two variable

$ TMP=/mount_point/tmp
$ TMPDIR=/mount_point/tmp
$ export TMP TMPDIR
  • For the C shell:
% setenv TMP /mount_point/tmp
% setenv TMPDIR /mount_point/tmp

The export or setenv word makes the variable to be available to all child sessions initiated from the current session

Permanently

You must change the shell startup script.

Delete

One

unset myEnvironmentVariable

All

With the env utility, you can start with a empty environment with the -i option.

env -i yourScript

Operations

See Bash - Parameter

Check if set

if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi

See Parameter check for more info.

Read-only

Bash - Declare (Variable declaration and attribute)

declare -r variableName

Documentation / Reference





Discover More
Azure Firewall Public Ip Restriction
Azure - Oracle Database Manually Installation on Linux

The installation of the Oracle database on bare-bone Linux. For a quick an dirty installation, see Creation of a VM with Oracle Linux 7 Update 4 and a Standard_DS1_v2...
Bash Liste Des Attaques Ovh
Bash - History (builtin command)

history is a Builtin Command that shows the history of the command executed. A space at the start of a command will stop the command (and any password) from being stored in the history. With...
Bash Liste Des Attaques Ovh
Bash - (Argument|Positional Parameter)

An argument is a parameter given: to a command to a function or to the bash shell They are referenced by position. A positional parameter is a parameter denoted: by one or more digits, ...
Bash Liste Des Attaques Ovh
Bash - (Builtin|Intern|System|Reserved|Shell) variable name

Reserved variable name are named that have a special meaning for the bash shell. PS1 defines the shell's command-line prompt. HOME defines the home directory for a user. PATH defines a list...
Bash Liste Des Attaques Ovh
Bash - (Name|Identifier)

A word consisting only of alphanumeric characters andunderscores, and beginning with an alphabetic character or an underscore. Also referred to as an identifier. A name identify: a variable ...
Bash Liste Des Attaques Ovh
Bash - Cd (Change Directory) builtin command

in bash. Cd is a builtin command that changes the current directory where: the options -L forces symbolic links to be followed. -P says to use the physical directory structure instead of...
Bash Liste Des Attaques Ovh
Bash - Complete (Builtin command) - Completion

The programmable completion feature in Bash permits typing a partial command, then pressing the [Tab] key to auto-complete the command sequence The process of applying these completion specifications...
Bash Liste Des Attaques Ovh
Bash - Declare (Variable declaration and attribute)

Declare variables and/or give them attributes. where: names: If no names are given then display the values of variables. p will display the attributes and values of each name. When -p is used,...
Bash Liste Des Attaques Ovh
Bash - Echo (Bultin Command)

Echo in bash. Echo accepts an argument (a literal string or a variable) as standard input, and echoes it back to the terminal as standard output. In Bash, it's available as: the builtin bash command...
Bash Liste Des Attaques Ovh
Bash - Export (Builtin command)

The supplied names (variable or function) are marked for automatic export to the environment of subsequently executed commands. declaringx -f: the names refer to functions. -p will print a...



Share this page:
Follow us:
Task Runner