English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Commande declare de Linux

Linux Command大全

La commande declare de Linux est utilisée pour déclarer des variables shell.

declare est une instruction shell, utilisée dans la première syntaxe pour déclarer des variables et définir leurs attributs ([rix] est l'attribut de la variable), et dans la deuxième syntaxe pour afficher les fonctions shell. Sans aucun paramètre, il affiche toutes les variables et fonctions shell (comme l'effet de l'instruction set).

grammaire

declare [+/-[rxi] [nomVariable = valeurDéfinie] ou declare -f

Parameter Description:

  • +/-  "-"can be used to specify the attributes of the variable,"+"is used to cancel the attributes set for the variable.
  • -f  Only display functions.
  • r  Set the variable as read-only.
  • x  The specified variable will become an environment variable, which can be used by programs outside of shell.
  • i  [Setting] can be a number, string, or expression.

Online Examples

Declare Integer Variable

# declare -i ab //Declare Integer Variable
# ab=56 //Change Variable Content
# echo $ab //Display Variable Content
56

Change Variable Attribute

# declare -i ef //Declare Integer Variable
# ef=1  //Variable Assignment (Integer Value)
# echo $ef //Display Variable Content
1
# ef="wer" //Variable Assignment (Text Value)
# echo $ef 
0
# declare +i ef //Remove Variable Attributes
# ef="wer"
# echo $ef
wer

Set Variable Read-only

# declare -r ab //Set Variable as Read-only
# ab=88 //Change Variable Content
-bash: ab: Read-only variable
# echo $ab //Display Variable Content
56

Declare Array Variable

# declare -a cd='([0]="a" [1]="b" [2]="c")' //Declare Array Variable
# echo ${cd[1]}
b //Display Variable Content
# echo ${cd[@]} //Display the Entire Array Variable Content
a b c

Display Function

# declare -f
command_not_found_handle () 
{ 
  if [ -x /usr/lib/command-not-found ]; then
    /usr/bin/python /usr/lib/command-not-found -- $1;
    return $?;
  else
    if [ -x /usr/share/command-not-found ]; then
      /usr/bin/python /usr/share/command-not-found -- $1;
      return $?;
    else
      return 127;
    fi;
  fi
}

Linux Command大全