Variable examples of how to output variables in a shell script

Source: Internet
Author: User


Problem background:

#!/bin/sh
php=100;
Java=101
Language= "PHP JAVA"
For Lang in $LANGUAGE
Todo
What do you want to do with 100, 101 line-by-row output?
Done

Workaround-Numeric string:

#!/bin/sh php=100;
Java=101
Language= "PHP JAVA" for Lang in $LANGUAGE do
Echo $[$lang]
Done

Workaround-Letter string:

#!/bin/sh
Language= (PHP JAVA)
Language_value= (/DATA/1/DATA/2)
For ((i=0;i< ${#LANGUAGE [@]};i++))
Todo
echo ${language_value[$i]}
Done


Shell script Export Environment variables


If you want to use a bash script to export some environment variables to the bash shell, you will encounter some problems. Because each bash script is the only one session, these environment variables will only work in their own scripts, and for the next environment variable to be used, the bash shell does not exist.

For example:

The following statement is included in the env.sh script:

Export Hello=hello
Export Hello2=world
When you're finished running env.sh, then execute echo $HELLO $HELLO 2 in the bash shell, you won't get anything.

The correct way to export an environment variable is to use the source command to export the environment variables from your bash script.

$ source Env.sh
$ echo $HELLO $HELLO 2
$ Hello World
You can put the env.sh script into the/usr/bin directory, then execute source env.sh on any path, and it works well. Here you will use the source command instead of executing the run script directly. But I realize that many users are unaware of the role of the source command.

In the Linux world, there is another way to use these environment variables in a single environment without affecting the use of other shells. It is

Bash-i

Create a new interactive bash session through the Bash script, and exit when you're not using it.
Here is an example:
############################################
build_arm.sh

#!/bin/bash
Export Arch=arm
Export cross_compile=arm-none-linux-gnueabi-
Export ps1= "\e[31mbuild_arm\w\e[m\n\$"
############################################

$ bash-i

Then
$./build_arm.sh

Build_arm ~ $

When you're not using it,
Build_arm ~ $ exit

$

This will exit to the first bash shell.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.