Bash - Variable

此生再无相见时 提交于 2019-12-12 07:37:44

In my opinion, Bash is not clear language. It is very brief but also hard to use because it has many detailed designs. Today Iet's invastigate how to use variable in bash script.

 

Define and use variable


# Note that there is no blank around equal sign.
var='abc'
VAR='def'

# we can use $ to quote the variable
echo $var
echo $VAR
echo $var 123 'bac'

libpath=/proj/lib
ls $libpath

Then we can see it echoes different value. That means bash shell is a case sensitive environment. You can use set variable name with uppercase or lowercase or a mixture of both. 

Every bash shell or script will inherit the environment variable from the parent process. You can use these environment variables directly, same as your defined variable.

echo $PATH
echo $HOME
echo $PYTHONPATH

Some special variables


For easy to write a bash script, system sets some variables to transfer some value.

  • $0        The name of the script
  • $1-9     The arguments that are transfered to this script.
  • $@      The all arguments supplied to script
  • $#        The number of the arguments
  • $?        The recent run process's  PID
  • $$        The current script process's PID

And there still is some special environment supported from environment. 

  • $USER                    Current user
  • $HOSTNAME          The host name of the machine that script is running on
  • $SECONDS             How long the script runs
  • $RANDOM               Return a random integer number
  • $LINENO                 The line number in this script

 

Command substitution


Command substitution allows us to set the output of a command into a variable

files=$( ls )
filesCount=$( ls | wc -l)

echo 'file:' $files 'count:' $filesCount
# output: file: bin lib tools count: 3

Disambiguate


Sometimes we may be couriout about how to refer to the tenth arguments, $10 ? No, it just print $1 and '0'. It must have a way to specify the detailed variable name to refer to the variable. {} can help us.

# ./demo.sh 1 2 3 4 5 6 7 8 9 a b c

echo $1       # print 1
echo $10      # print 10 ("$1"+"0")
echo ${10}    # print a

foo=abc
foobar=def
echo $foobar      # print def
echo ${foo}bar    # print abcbar

Export variable


Before, we said that we could use the environment variable and the environment variables can be transfered to its child process. Bash support a way to export its variable to current environment and pass to its child process. Lets check this demo.

#! /bin/bash
#  script1.sh try to export variable

var1=abc
var2=def

echo  $0 'var1:' $var1 'var2:' $var2

export $var1

./script2.sh

echo  $0 'var1:' $var1 'var2:' $var2
#! /bin/bash
#  script2.sh use env variable and try to modify it

echo  $0 'var1:' $var1 'var2:' $var2

var1=fake

Then we check the output :

script1.sh var1: abc var2:def
script1.sh var1: abc var2:
script1.sh var1: abc var2:def

The conclusion is that child process only can transter value by the environment variable, child process can't affect mother process's variable because it just exports a copied value to env.

 

Sources:

 https://ryanstutorials.net/bash-scripting-tutorial/bash-variables.php

 http://www.ojit.com/article/2567958

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!