- Learning Linux Shell Scripting
- Ganesh Naik
- 419字
- 2021-06-25 22:02:55
Exporting variables
We can use the export command to make variables available in the child process or subshell. But if we declare new variables in the child process and export it in the child process, the variable will not be available in parent process. The parent process can export variables to a child, but the child process cannot export variables to the parent process.
Whenever we create a Shell script and execute it, a new shell process is created and the Shell script runs in that process. Any exported variable values are available to the new shell or to any sub-process.
We can export any variable as follows:
$ export NAME
Or we can use this:
$ declare -x NAME
Let's try to understand the concept of exporting the variable, using the following example:
$ PERSON="Ganesh Naik"$ export PERSON$ echo $PERSONGanesh Naik$ echo $$515
The process ID of the current shell or parent shell is 515.
This will start a subshell:
$ bash
This is the process ID of new or sub-shell:
$ echo $$526
Let us check the presence of variables:
$ echo $PERSONGanesh Naik$ PERSON="Author"$ echo $PERSONAuthor$ exit
This will terminate the subshell, and it will be placed in the parent shell:
$ echo $$
515
This displays the presence of the variable in the original shell or parent shell:
$ echo $PERSONGanesh Naik
Let's write a shell script to use the concept we have learned:
# Ubuntu Timezone files location : /usr/share/zoneinfo/ # redhat "/etc/localtime" instead of "/etc/timezone" # In Redhat # ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime export TZ=America/Los_Angeles echo "Your Timezone is = $TZ" date export TZ=Asia/Tokyo echo "Your Timezone is = $TZ" date unset TZ echo "Your Timezone is = $(cat /etc/timezone)" # For Redhat or Fedora /etc/localtime date
The date command checks the TZ environmental variable. We initialized the TZ for Los_Angeles, then to Tokyo, and, finally, we removed it. We can see the difference in the date command output.
Let's write another Shell script to study the parent and child process, and the export of variables.
Create the export1.sh shell script:
#!/bin/bash foo="The first variable foo" export bar="The second variable bar" ./export2.sh Create another shell script export2.sh #!/bin/bash echo "$foo" echo "$bar"
The shell script export1.sh runs as a parent process and export2.sh is started as a child process of export1.sh. We can clearly see that variable bar, which was exported, is available in the child process, but the variable foo, which was not exported, is not available in the child process.
- 3D Printing with RepRap Cookbook
- Blockchain Quick Start Guide
- Matplotlib 3.0 Cookbook
- 計(jì)算機(jī)網(wǎng)絡(luò)技術(shù)實(shí)訓(xùn)
- 計(jì)算機(jī)網(wǎng)絡(luò)技術(shù)基礎(chǔ)
- Apache Spark Deep Learning Cookbook
- 大數(shù)據(jù)驅(qū)動(dòng)的設(shè)備健康預(yù)測(cè)及維護(hù)決策優(yōu)化
- 變頻器、軟啟動(dòng)器及PLC實(shí)用技術(shù)260問(wèn)
- Kubernetes for Serverless Applications
- Storm應(yīng)用實(shí)踐:實(shí)時(shí)事務(wù)處理之策略
- 水下無(wú)線傳感器網(wǎng)絡(luò)的通信與決策技術(shù)
- 悟透AutoCAD 2009案例自學(xué)手冊(cè)
- 電子設(shè)備及系統(tǒng)人機(jī)工程設(shè)計(jì)(第2版)
- 中文版AutoCAD 2013高手速成
- PowerMill 2020五軸數(shù)控加工編程應(yīng)用實(shí)例