Bash - Variable

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值